简体   繁体   中英

How do I remove a “&” symbol from a URL using regular expressions?

how to remove a & Symbol from a url address use php regular? for example: http://www.google.com/search?hl=en&q=php left the & and get : http://www.google.com/search?hl=enq=php Thanks

$url = 'http://www.google.com/search?hl=en&q=php';
$url = str_replace('&', '', $url);

I'm not sure I really understand the question. It sounds like you just want to remove & characters. That can be easily done:

$url = str_replace('&', '', $url);

You can remove it easily enough with str_replace. Why you would want to do this, however, is another matter entirely.

Are you trying to insert a URL into a page with PHP and getting validation errors due to the & symbol? In that case urlencode() might be what you really need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM