Page 1 of 1

str_replace or preg_replace?

Posted: Fri Apr 04, 2008 3:39 pm
by thiscatis
Hi guys,

I'm having some troubles here...


I'm checking an URL to see if it contains '&number=1' where '1' is a variable.
(I'm doing this with isset)

and if the URL contains that string with the variable number at the end it should remove the entire string.
I've managed to do this with a constant string with the str_replace() function. But I can't manage to find
a way to remove the bit if it contains a variable number at the end...

Any suggestions?

Thanks in advance!


thiscatis

Re: str_replace or preg_replace?

Posted: Fri Apr 04, 2008 4:08 pm
by s.dot
Yeah, preg_replace(). \d will check for a number

Re: str_replace or preg_replace?

Posted: Fri Apr 04, 2008 6:39 pm
by thiscatis
$this_safe_page = preg_replace('&number=/[\d]/', '', $this_page);


gives me this:

Warning: preg_replace() [function.preg-replace]: No ending delimiter '&' found in ...

:banghead:

Re: str_replace or preg_replace?

Posted: Fri Apr 04, 2008 9:24 pm
by John Cartwright
Because you need to specify the delimers ;)
preg_replace(#'&number=/[\d]/#', '', $this_page);
.. which can be any character (which have to be at the front and end of the pattern)

Re: str_replace or preg_replace?

Posted: Sat Apr 05, 2008 4:18 am
by thiscatis
Edit,

Code: Select all

$this_safe_page = preg_replace('/\&number=\d/', '', $this_page);
this did the trick

Re: str_replace or preg_replace?

Posted: Sat Apr 05, 2008 4:53 am
by Chris Corbyn
If there will ever be more than one digit in the number add a + sign:

Code: Select all

'/&number=\d+/'
EDIT | The ampersand doesn't need escaping since it has no special meaning to the regular expression engine ;)