Posted By

nilambar on 09/26/10


Tagged

php


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

Calaelen


Get slug(URL friendly string)


 / Published in: PHP
 

Get url friendly string

  1. /*
  2. * getSlug()
  3. * @action:return cleaned text of given string
  4. * Used to get url friendly title
  5. * @parameters:
  6. * $str: string to be cleaned
  7. * $replace: array of characters to be cleaned; default empty
  8. * $delimiter: delimiter to separate words; default is '-'
  9. * @return: string of given length
  10. * @modified : 19 September 2010
  11. * @modified by: Nilambar
  12. */
  13. function getSlug($str, $replace=array(), $delimiter='-') {
  14. setlocale(LC_ALL, 'en_US.UTF8');
  15. //
  16. if( !empty($replace) ) {
  17. $str = str_replace((array)$replace, ' ', $str);
  18. }
  19. $clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
  20. $clean=strip_tags($clean);
  21. $clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
  22. $clean = strtolower(trim($clean, '-'));
  23. $clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
  24. return $clean;
  25. }

Report this snippet  

You need to login to post a comment.