regex help please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

regex help please!

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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..
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

ah I didn't know str_replace took arrays, cool :) fore some reason I thought just preg did that..
Post Reply