Remove & and Whitespace in URL with preg_replace

2019-07-29 17:56发布

I am trying to remove Whitespaces and & in a URL and replace it with a -. So far the following works:

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

I would like to have this in one line, but I am not able to combine the 2. Can anyone help? Thank you very much in advance.

5条回答
forever°为你锁心
2楼-- · 2019-07-29 18:08
$output = preg_replace('/([\s+|\&])/', '-', $page->label);
查看更多
聊天终结者
3楼-- · 2019-07-29 18:12

This should keep it all nicely in one line.

$output = preg_replace('/\s+|&/', '-', $page->label);
查看更多
来,给爷笑一个
4楼-- · 2019-07-29 18:16

if you use Zend Framework, maybe Zend View Escaspe is better.

查看更多
对你真心纯属浪费
5楼-- · 2019-07-29 18:21
preg_replace( '/[&]|\s+/', '-', $page->label );
查看更多
何必那么认真
6楼-- · 2019-07-29 18:32
 $test = preg_replace('/\s+/', '-', $page->label);  
 $final_output = preg_replace('/\&/', '-', $test); 

Try like this

查看更多
登录 后发表回答