如何生成SEF slu or或url

时间:2012-11-11 09:56:10

标签: php mysql sef

我想像wordpress一样生成页面,类别等的slu .. 让我有页面标题:“我的页面标题” 然后转义空格和特殊字符并生成类似“my-page-title” 怎么做

1 个答案:

答案 0 :(得分:1)

这是生成slugs的函数

function slug($var){
    $seoname = preg_replace('/\%/',' percentage',$var); 
    $seoname = preg_replace('/\@/',' at ',$seoname); 
    $seoname = preg_replace('/\&/',' and ',$seoname); 
    $seoname = preg_replace('/\s[\s]+/','-',$seoname);    // Strip off multiple spaces 
    $seoname = preg_replace('/[\s\W]+/','-',$seoname);    // Strip off spaces and non-alpha-numeric 
    $seoname = preg_replace('/^[\-]+/','',$seoname); // Strip off the starting hyphens 
    $seoname = preg_replace('/[\-]+$/','',$seoname); // // Strip off the ending hyphens 
    $seoname = strtolower($seoname); 

    return $seoname;
}