PHP - Clean up a string

A small PHP function which can be quite useful, especially when doing the URL rewriting. This function transforms any accented characters of any string , removing special characters and replacing empty spaces by dashes.

function string2url($string) {  
 $chaine = trim($string);  
 $chaine = strtr($string,  
 $chaine = strtr($string,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz");  
 $chaine = preg_replace('#([^.a-z0-9]+)#i', '-', $string);  
        $chaine = preg_replace('#-{2,}#','-',$string);  
        $chaine = preg_replace('#-$#','',$string);  
        $chaine = preg_replace('#^-#','',$string);  
 return $string;  

Example of use:
echo string2url(Don't you think Kioskea is great!!);   
// Will give as result: don-t-you-think-ccm-is-great

Published by jak58. Latest update on January 3, 2013 at 05:51 AM by jak58.
This document, titled "PHP - Clean up a string," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (
PHP - antispam code with numbers and letters (CAPTCHA)
PHP script listing file directory