2012-02-23 38 views
0

我想我的問題很簡單,但我新的PHP,所以我失去了與此有關。基本上我想用短劃線(「 - 」)替換空格(「」),並讓錨文本爲小寫,因爲它將用作url的一部分。轉換錨文本 - 的preg_replace,正則表達式

例:錨文本「莎士比亞」將被轉換爲「威廉莎士比亞」要插入作爲URL的一部分。

<a href="http://site.com/biography/william-shakespeare">William Shakespeare</a> 

如果文本只有一個字將被轉換爲小寫 例:「莎士比亞」會被轉換爲「莎士比亞」。

<a href="http://site.com/tag/shakespeare">Shakespeare</a> 

注:文本使用GOOGLETRANSLATE葡萄牙語翻譯。

回答

1

無需使用正則表達式。只需使用str_replace & strtolower

strtolower(str_replace(' ', '-', $string)); 

你應該也urlencode它,如果你在那裏有一些靠不住的字符:

urlencode(strtolower(str_replace(' ', '-', $string))); 
+0

好,約瑟夫,謝謝你的提示。 – 2012-05-20 15:56:02