Advertisement
iarmin

php slugify / urlize

Dec 24th, 2011
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.63 KB | None | 0 0
  1. # PHP slugify
  2.  
  3. function slugify( $text, $unique_id = null ) {
  4.   $old_locale = setlocale( LC_CTYPE, 0 );
  5.   if( $old_locale != "en_US.UTF-8" ) {
  6.     setlocale( LC_CTYPE, "en_US.UTF-8" );
  7.     $old_locale = null;
  8.   }
  9.  
  10.   $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  11.   $text = trim($text, '-');
  12.  
  13.   if (function_exists('iconv')) { $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); }
  14.  
  15.   $text = strtolower($text);
  16.   $text = preg_replace('~[^-\w]+~', '', $text);
  17.  
  18.   if( $unique_id !== null ) {
  19.     $text .= "-".$unique_id;
  20.   }
  21.  
  22.   if( $old_locale !== null ) {
  23.     setlocale( LC_CTYPE, $old_locale );
  24.   }
  25.   return $text;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement