我有這個功能,從字符串生成SEO友好的URL:如何提高我的搜索引擎優化的URL生成
function seo_titleinurl_generate($title)
{
$title=substr($title,0,160);
$title = ereg_replace(" ", "-", $title); // replace spaces by "-"
$title = ereg_replace("á", "a", $title); // replace special chars
$title = ereg_replace("í", "i", $title); // replace special chars
$title = ereg_replace("ó", "o", $title); // replace special chars
$title = ereg_replace("ú", "u", $title); // replace special chars
$title = ereg_replace("ñ", "n", $title); // replace special chars
$title = ereg_replace("Ñ", "n", $title); // replace special chars
$title = strtolower(trim($title)); // lowercase
$title = preg_replace("/([^a-zA-Z0-9_-])/",'',$title); // only keep standard latin letters and numbers, hyphens and dashes
if($title=="" or $title=="-"){
$mr=rand(1,99999);
$mt=time();
$title=$mr.$mt;
}
return $title;
}
但在某些情況下,當字符串中有多個空格,如:最(3 spaces here)
不錯的惡作劇! 它的產生:最惡劣的惡作劇
我希望它忽略許多空間,並使他們只有一個破折號。
感謝
只是字符替換,str_replace函數和如更快的我不會用ereg_replace。 – neo 2010-01-18 16:39:50