2010-01-18 227 views
1

我有这个功能,从字符串生成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)不错的恶作剧! 它的产生:最恶劣的恶作剧

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

感谢

+1

只是字符替换,str_replace函数和如更快的我不会用ereg_replace。 – neo 2010-01-18 16:39:50

回答

1

我想这可能是比以前的答案要快一点,因为它不会与单个空格浪费时间(我可能是错的) :

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

我认为他们试图从字符串中删除空格,并将其改为破折号 – 2010-01-18 16:56:13

0

只需添加开头:

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

ereg_ *已过时,将在PHP 6中删除。请改用preg_ *。 – 2010-01-18 16:41:27

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; 
} 
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。这样,也许你可以在某处记录无效标题并在稍后修复。使用现在的函数,您只会得到一个数值返回值,并不真正知道它是否是无效标题的结果,或者它是否是一个恰好有数字的有效标题。

+0

如果您打算低估正确的答案,至少要善于解释原因。 – 2010-01-18 20:03:24

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