fork download
  1. <?php
  2.  
  3. function slugify($text)
  4. {
  5. // replace non letter or digits by -
  6. $text = preg_replace('~[^\pL\d]+~u', '-', $text);
  7.  
  8. // transliterate
  9. $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
  10.  
  11. // remove unwanted characters
  12. $text = preg_replace('~[^-\w]+~', '', $text);
  13.  
  14. // trim
  15. $text = trim($text, '-');
  16.  
  17. // remove duplicate -
  18. $text = preg_replace('~-+~', '-', $text);
  19.  
  20. // lowercase
  21. $text = strtolower($text);
  22.  
  23. if (empty($text)) {
  24. return 'n-a';
  25. }
  26.  
  27. return $text;
  28. }
  29.  
  30.  
  31. $str1 = "This is a test url - hyphen problem. When there is space before a character.";
  32. $str2 = "";
  33.  
  34.  
  35. echo slugify($str2);
  36.  
  37.  
  38.  
  39.  
Success #stdin #stdout 0.02s 24604KB
stdin
Standard input is empty
stdout
n-a