2010-04-07 45 views
0

嘿,我只是想知道是否有任何簡單的功能,使文本適合在一個鏈接,說我有一堆帽,奇怪的人物等,我希望它用「 - 」代替空格是小寫,有沒有這樣的功能呢,還是我必須創建自己的?任何簡單的功能SEO文本的鏈接PHP

回答

2

試試這一個,從snipplr

function slug($str) 
{ 
    $str = strtolower(trim($str)); 
    $str = preg_replace('/[^a-z0-9-]/', '-', $str); 
    $str = preg_replace('/-+/', "-", $str); 
    return $str; 
} 
0
$url = "http://hellO.com/you re here/right.html";  
//get rid of spaces 
$url = str_replace(" ", "-", $url); 
//make lowercase 
$url = strtolower($url); 

這會給你 「http://hello.com/you-re-here/right.html

照此邏輯處理這一怪異字符。

0

您可以嘗試preg_replace(http://php.net/manual/en/function.preg-replace.php),它使用正則表達式與strtolower結合使用。 這使得一切都變成小寫,然後將不是小寫字母(如空格)的任何內容轉換爲連字符。

$str = preg_replace("/[^a-z]/", "-", strtolower($str));