Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

str_replace + regular expression

Tags:

regex

php

I have:

http://stackoverflow.com/questions/43242&cat=aa&id=342342
http://stackoverflow.com/questions/43242&cat=aa&body=434&id=232
http://stackoverflow.com/questions/43242&cat=aa&call=2323&id=14143434

i would like receive:

this link without parameter id:

http://stackoverflow.com/questions/43242&cat=aa
http://stackoverflow.com/questions/43242&cat=aa&body=434
http://stackoverflow.com/questions/43242&cat=aa&call=2323

how this make with PHP? str_replace + regular expression?

like image 519
Paul Sloppy Avatar asked Dec 27 '22 14:12

Paul Sloppy


2 Answers

preg_replace('~&id=[0-9]+~', '', $str);
like image 54
genesis Avatar answered Jan 08 '23 05:01

genesis


Use the appropriate function for this, not regular expressions since URL's aren't regular.

You should then split the query part, which can be done with regular expressions, but I'd like to split on & and then filter out the ID part.

like image 39
CodeCaster Avatar answered Jan 08 '23 07:01

CodeCaster