¡Hola amigos!
Hasta hoy, al momento de crear strings, me encontraba utilizando str_replaces y un montón de cosas mas, cosas que siempre me llevaban a urls amigables un tanto feas.
Hoy busqué y encontré una función que permite crear slugs fácilmente, y que además convierte letras del tipo ‘á’ a ‘a’.
Sin mas, el código:
function slugify($text, $encoding = "utf-8") { $text = preg_replace ( '~[^\\pL\d]+~u', '-', $text ); $text = trim ( $text, '-' ); if (function_exists ( 'iconv' )) { $text = iconv ( $encoding, 'us-ascii//TRANSLIT', $text ); } $text = strtolower ( $text ); // remove unwanted characters $text = preg_replace ( '~[^-\w]+~', '', $text ); if(empty($text)) { return false; } return $text; }
¡Espero que les sea de utilidad!
Un saludo,
Pedro