Get slug(URL friendly string)


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

Get url friendly string


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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.