所以,我有这个方法:
$seo_special = array("ä", "õ", "ö", "ü", "Ä", "Õ", "Ö", "Ü");
$seo_special_replace = array("a", "o", "o", "u", "a", "o", "o", "u");
$seo_tag_a2b = str_replace($seo_special, $seo_special_replace, $row->name);
$seo_tag_a2b = preg_replace('/[^a-z0-9-]/i', '', strtolower(str_replace(' ', '-', trim($seo_tag_a2b))));
我希望它能将特殊字符替换成普通字符,但不知何故,它跳过了这些字符,而p_randalaud/liistparkett的结果是prandalaud——liistparket应该是porandalaud——liistparket,我不知道我做错了什么。任何帮助都将不胜感激。
最佳答案
您可以使用iconv将奇怪的字符转换为仅限ascii的字符。
setlocale(LC_ALL, 'en_US.UTF8');
function toAscii($str, $replace=array(), $delimiter='-') {
if( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
测试:
echo toAscii("Mess'd up --text-- just (to) stress /test/ ?our! `little` \\clean\\ url fun.ction!?-->");
returns: messd-up-text-just-to-stress-test-our-little-clean-url-function
echo toAscii("Perché l'erba è verde?", "'"); // Italian
returns: perche-l-erba-e-verde
echo toAscii("Peux-tu m'aider s'il te plaît?", "'"); // French
returns: peux-tu-m-aider-s-il-te-plait
echo toAscii("Tänk efter nu – förr'n vi föser dig bort"); // Swedish
returns: tank-efter-nu-forrn-vi-foser-dig-bort
echo toAscii("ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöùúûüýÿ");
returns: aaaaaaaeceeeeiiiidnooooouuuuyssaaaaaaaeceeeeiiiidnooooouuuuyy
echo toAscii("Custom`delimiter*example", array('*', '`'));
returns: custom-delimiter-example
echo toAscii("My+Last_Crazy|delimiter/example", '', ' ');
returns: my last crazy delimiter example
方法和学分The perfect PHP clean url generator
为了帮助你搜索,你可以搜索“url slug php”。