Php 如何改进我的搜索引擎优化url生成器

Php 如何改进我的搜索引擎优化url生成器,php,seo,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", $

我有一个函数,它可以从字符串生成一个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;
     }
但是在某些情况下,当字符串有多个空格时,比如:most
(这里有3个空格)
很好的恶作剧! 这是最有趣的恶作剧

我想让它忽略很多空格,只做一个破折号


谢谢

只需在开头添加:

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

只需在开头添加:

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

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


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


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


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


我建议如下:

/**
* 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。通过这种方式,也许您可以在某个地方对无效标题进行记录,并在以后对其进行修复。使用现在的函数,您只会得到一个数字返回值,并不知道这是无效标题的结果,还是碰巧充满数字的有效标题。

我建议如下:

/**
* 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。通过这种方式,也许您可以在某个地方对无效标题进行记录,并在以后对其进行修复。使用现在的函数,您只会得到一个数字返回值,并不知道这是无效标题的结果,还是碰巧充满数字的有效标题。

请查看以下代码:

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'))), '-'));
}

请看下面的代码:

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'))), '-'));
}

我不会将ereg_替换用于字符替换、str_替换等更快的操作。我不会将ereg_替换用于字符替换、str_替换等更快的操作。ereg_*已被弃用,将在PHP 6中删除。使用preg_*代替。ereg_*已被弃用,将在PHP6中删除。使用preg_*代替。我认为他们正在尝试从字符串中删除空格并将其更改为破折号我认为他们正在尝试从字符串中删除空格并将其更改为破折号如果你要否决正确答案,至少要解释一下原因。如果你要否决正确答案,至少要解释一下原因。