php regural exp remove special chars
i have the follow code to create a string without special chars and separate with - because its used in the url
$txt = preg_replace("/[^a-z0-9\s-]/", "", $txt);
$txt = trim(preg_replace("/[\s-]+/", " ", $txt));
$txt = trim(mb_substr($txt, 0, 1024));开发者_如何学C
$txt = preg_replace("/\s/", "-", $txt);
Its was working fine so far, but today when migrate form other database i discover some fails.
When txt has (½) superscript fails
When txt has symbols like ° (temperature), ® (copyright), ™ (trademark) fails
When txt has ... also fails (that's the most strange bucause if has 1 dot working)
Can anyone help please
Thanks
Try this:
function friendlyURL($string){
$string = preg_replace("`\[.*\]`U","",$string);
$string = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$string);
$string = htmlentities($string, ENT_COMPAT, 'utf-8');
$string = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig|quot|rsquo);`i","\\1", $string );
$string = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $string);
return strtolower(trim($string, '-'));
}
$myFriendlyURL = friendlyURL("Barca rejects FIFA statement © on Olympics row ®");
echo $myFriendlyURL; // will echo barca-rejects-fifa-statement-on-olympics-row
I am not sure what you are trying to do but
str_replace($string,$txt);
should work
精彩评论