Forum Moderators: coopster

Message Too Old, No Replies

preg replace end of line

almost there!

         

wesg

12:44 am on Aug 1, 2008 (gmt 0)

10+ Year Member



Thanks to everyone on WebmasterWorld, I'm almost done my annoying little problem.

Right now I'm using preg_replace to modify URLS to properly display. What is causing me grief is that I have multiple GET variables in play.

Imagine my URL is this :
.../?show=50&order=ASC

My program will be able to take that current URL and change one variable to something else without modifying the other variables.
ie.
.../?show=100&order=ASC

Right now I can successfully modify the inside variable, but cannot change the last one.
My regex statement is this:

preg_replace('/' . $var . '=([^&]+)[&]/', $result, $URL);'

where $var is the variable I'm changing, $result is the proper URL, and $URL is the input string. As you can imagine, it replaces properly on the inside because the code reaches the & character, but not when it's the last one.

What can I add to make it stop at the & or at the end of line?

eelixduppy

3:50 am on Aug 1, 2008 (gmt 0)



Try a pattern like this:

'/' . $var . '=([^&]+)/'

Also, can't you just use a simple str_replace here? I don't see why you are using regex.

wesg

8:39 pm on Aug 1, 2008 (gmt 0)

10+ Year Member



I'm using regex because I need to replace both the variable, and the value, which changes. If the value remained the same, I would use str_replace.

The reason I need to use the end of line character is because the variables don't seem to replace when they're on the end.