La funzione ha il vantaggio ulteriore di operare indipendentemente dalla codifica della stringa. Buon lavoro!
URL Slug – Funzione PHP
1 2 3 4 5 6 7 8 9 10 11 |
function to_permalink($str) { if($str !== mb_convert_encoding( mb_convert_encoding($str, 'UTF-32', 'UTF-8'), 'UTF-8', 'UTF-32') ) $str = mb_convert_encoding($str, 'UTF-8', mb_detect_encoding($str)); $str = htmlentities($str, ENT_NOQUOTES, 'UTF-8'); $str = preg_replace('`&([a-z]{1,2})(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i', '\\1', $str); $str = html_entity_decode($str, ENT_NOQUOTES, 'UTF-8'); $str = preg_replace(array('`[^a-z0-9]`i','`[-]+`'), '-', $str); $str = strtolower( trim($str, '-') ); return $str; } |
Esempio di utilizzo
1 2 3 |
$str = "J'adôre les Sushis."; $str = to_permalink($str); echo $str; // j-adore-les-sushis |
pixolo ads