2012-02-24 78 views
1

我試圖刪除URL中的空格和&,並用 - 替換它。到目前爲止,以下工作:刪除URL中的空格和preg_replace

preg_replace('/\s+/', '-', $page->label) // whitepace gets replaced with -  
preg_replace('/\&/', '-', $page->label) // & gets replaced with - 

我想這一行,但我不能結合2.任何人都可以幫助嗎? 非常感謝您提前。

回答

6

這應該保持一行很好。

$output = preg_replace('/\s+|&/', '-', $page->label); 
+0

偉大的作品!非常感謝你。 – Luka 2012-02-24 12:20:51

0
$test = preg_replace('/\s+/', '-', $page->label); 
$final_output = preg_replace('/\&/', '-', $test); 

嘗試這樣

0
preg_replace('/[&]|\s+/', '-', $page->label); 
0
$output = preg_replace('/([\s+|\&])/', '-', $page->label);