Posted By

designfellow on 07/16/16


Tagged


Versions (?)

Convert space to dash


 / Published in: PHP
 

Convert space to dash

  1. function url_title($str, $separator = '-', $lowercase = FALSE)
  2. {
  3. if ($separator === 'dash')
  4. {
  5. $separator = '-';
  6. }
  7. elseif ($separator === 'underscore')
  8. {
  9. $separator = '_';
  10. }
  11. $q_separator = preg_quote($separator, '#');
  12. $trans = array(
  13. '&.+?;' => '',
  14. '[^\w\d _-]' => '',
  15. '\s+' => $separator,
  16. '('.$q_separator.')+' => $separator
  17. );
  18. $str = strip_tags($str);
  19. foreach ($trans as $key => $val)
  20. {
  21. $str = preg_replace('#'.$key.'#i'.(UTF8_ENABLED ? 'u' : ''), $val, $str);
  22. }
  23. if ($lowercase === TRUE)
  24. {
  25. $str = strtolower($str);
  26. }
  27. return trim(trim($str, $separator));
  28. }

Report this snippet  

You need to login to post a comment.