我正在編寫一個自定義的博客腳本,並且作爲它的一部分,我正在使用用於發佈URL的slug。我有它的代碼到目前爲止是需要函數的輕微修改
public function get_slug($str) {
// convert to lowercase
$str = strtolower($str);
// remove special chars
$str = preg_replace('/[^a-zA-Z0-9]/i', ' ', $str);
// remove what space from beginning and end
$str = trim($str);
// remove repeat spaces
$str = preg_replace('/\s+/', ' ', $str);
// replace spaces with hyphens
$str = preg_replace('/\s+/', '-', $str);
return $str;
}
這偉大工程的大部分地區,但是如果在字中間的特殊字符它只是用連字符轉向「可以代替它,我已經注意到't'into'can-t'而不是'can not'
雖然在預設情況下,我可以編輯數據庫並手動修復它,我想自動從字的中間去掉特殊字符,不要用炒作來代替它們。
退房[此功能(http://stackoverflow.com/questions/3230012/:
上述前將這個用它們的對應物替換口音/ 3230193#3230193)作爲這一個的潛在替代品。 –
嘗試在第一次替換中將空格更改爲空字符串。 – Luigi