Convert space to dash


/ Published in: PHP
Save to your folder(s)

Convert space to dash


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.