我有以下代码来创建一个没有特殊字符的字符串并与之分开 - 因为它在url中使用
$txt = preg_replace("/[^a-z0-9\s-]/", "", $txt);
$txt = trim(preg_replace("/[\s-]+/", " ", $txt));
$txt = trim(mb_substr($txt, 0, 1024));
$txt = preg_replace("/\s/", "-", $txt);
到目前为止它工作得很好,但今天从其他数据库迁移时我发现有些失败了。
当txt有(½)上标失败时
当txt的符号如°(温度)时,®(版权),™(商标)失败
当txt有...也失败时(如果有一个点工作,这是最奇怪的事情)
任何人都可以帮忙吗
由于
答案 0 :(得分:0)
试试这个:
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
答案 1 :(得分:-2)
我不确定你要做什么但是
str_replace($string,$txt);
应该有效