quocvuongdn

#php: convert string UTF-8 to ASCII by Regex

Aug 17th, 2014
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.40 KB | None | 0 0
  1. function replace($text)
  2. {
  3. // replace non letter or digits by -
  4. $text = preg_replace('~[^\\pL\d]+~u', '-', $text);
  5.  
  6. // trim
  7. $text = trim($text, '-');
  8.  
  9. // transliterate
  10. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  11.  
  12. // lowercase
  13. $text = strtolower($text);
  14.  
  15. // remove unwanted characters
  16. $text = preg_replace('~[^-\w]+~', '', $text);
  17.  
  18. if (empty($text))
  19. {
  20. return 'n-a';
  21. }
  22.  
  23. return $te
Advertisement
Add Comment
Please, Sign In to add comment