如何改进我的Seo url生成器

时间:2010-01-18 16:35:56

标签: php seo

我有这个函数从字符串生成一个seo友好的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)个恶作剧! 它产生了:最多---好恶作​​剧

我希望它忽略许多空格并使它们只有一个破折号。

由于

5 个答案:

答案 0 :(得分:1)

我认为这可能比前一个答案快一点,因为它不会乱用单个空格(我可能错了):

$title = preg_replace('/\s\s+/', ' ', $title);

答案 1 :(得分:0)

只需在开头添加:

$title = ereg_replace(/\s+/, " ", $title); 

答案 2 :(得分:0)

这使用了preg_replace,因为ereg_replace已弃用并且将在未来的PHP版本中消失。它还使用数组来减少函数调用的数量,并使用str_replace进行一对一替换(速度更快):

function seo_titleinurl_generate($title)
{
        $title = substr(strtolower(trim($title)),0,160);

        $title = preg_replace('/\s+/', '-', $title); // replace spaces by "-"
        $title = str_replace(array("á","í","ó","ú","ñ","Ñ"), array("a","i","o","u","n","n"), $title);// replace special chars
        $title = preg_replace('/\W-/', '', $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 :(得分:0)

我建议如下:

/**
* Produce a title with lowercase alphanumeric characters, underscores, 
* and dashes.  There should be no instances of multiple concurrent dashes, 
* and no spaces.
*
* @param string $title the title being sanitized
*
* @return string the sanitized title, or a concatenation of a random 
*                number and the current time
*/
function seoTitleInUrlGenerate($title)
{
    $title = substr( 
                 preg_replace(
                     array("/([^a-zA-Z0-9_-])/", "/([--]{2,})+/"),
                     array('', '-'),
                     strtolower( strtr( trim($title), 'áéíóúñÑ ', 'aeiounN-' ) )
                 ), 0, 160
             );

    if ($title == "" or $title == "-")
    {
        return rand(1, 99999) . time();
    }
    else
    {
        return $title;
    }
 }

使用您提供的输入进行测试时...

echo seoTitleInUrlGenerate('the most    nice pranks!'); // "the-most-nice-pranks"

如果您无法在URL中生成有效的标题,我建议您返回FALSE,而不是返回随机数和时间。这样也许您可以在某处记录无效标题并在以后修复它。使用现在的功能,您只需获得一个数字返回值,并且不知道它是否是无效标题的结果,或者它是否是恰好充满数字的有效标题。

答案 4 :(得分:0)

看看以下代码:

function Slug($string)
{
    return strtolower(trim(preg_replace(array('~[^0-9a-z]~i', '~-+~'), '-', preg_replace('~&([a-z]{1,2})(acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8'))), '-'));
}