string_to_slug

Public

Convert a string to a slug

</> CopyGet raw version
php
  1. /**
  2.  * Converts a string to a slug, for use in URLs or CSS classes. This function properly replaces
  3. * letters with accents with their non-accented counterparts.
  4.  *
  5.  * @param string The string to convert.
  6.  * @return string The slug.
  7.  */
  8. function string_to_slug($string) {
  9. $string = str_replace(array('/', ' '), '-', $string);
  10. $accents = '/&([A-Za-z]{1,2})(tilde|grave|acute|circ|cedil|uml|lig);/';
  11. return urlencode(strtolower(preg_replace($accents, '$1', htmlentities(utf8_decode($string)))));
  12. }