2010-04-07 43 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));