Page 1 of 1

regex help please!

Posted: Mon Feb 24, 2003 10:12 am
by AbraCadaver
Hi all,

I'm using the following to try and replace urls in my html output:

$newhrefs = preg_replace("/script.php\?(.*)=(.*)&(.*)=(.*)&(.*)=(.*)/", "script-$1-$2-$3-$4-$5-$6.html", $hrefs);

This works fine as long as there are exactly 3 var=val pairs... not 1 or 2 or 4 or more...

How can I write my expression to match 1 or more pairs??? For example:

script.php?var=val
script.php?var=val&var2=val2
script.php?var=val&var2=val2&var3=val3
script.php?var=val&var2=val2&var3=val3&var4=val4
etc...etc...etc...

TIA,
Shawn

Posted: Mon Feb 24, 2003 7:13 pm
by Stoker
hmm what are you trying to do? Using regex to group and repat stuff in order to replace simple stuff inbetween the groups sound like the wrong way to me..

if it is about changing any &, = and ? to a - i would do that with four str_replace() instead..

Posted: Tue Feb 25, 2003 2:48 am
by twigletmac
Or one str_replace():

Code: Select all

$string = 'script.php?var=val&var2=val2&var3=val3&var4=val4';
$from = array('.php?', '=', '&');
$to = '-';
$replaced = str_replace($from, $to, $string);
Mac

Posted: Tue Feb 25, 2003 7:27 am
by Stoker
ah I didn't know str_replace took arrays, cool :) fore some reason I thought just preg did that..