2010-01-18 34 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'))), '-')); 
} 
相關問題