Forum Moderators: coopster

Message Too Old, No Replies

regex help please!

         

AbraCadaver

3:34 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



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

Robber

5:23 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Welcome to the boards AbraCadaver.

I guess you need to group the name/value pairs using () but I'm not sure how you would then go about using the backreferences for the replace.

Sounds like one for Andreas to me - or any of the other RegEx pros round here!

AbraCadaver

5:36 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Thanks!

I have it working like this now (don't know how good it is), but would like to combine into one preg_replace if that is the fastest way:

$search = array(
"/(script).php\?((\w+)=(\w+))/",
"/&((\w+)=(\w+))/");

$replace = array(
"$1-$3-$4",
"-$2-$3");

Thanks again!
Shawn

brotherhood of LAN

5:53 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I use this for links, it gets the anchor text aswell

$links = preg_match_all("'</?a ([^>]*)?href=[\"\']?([^\'\">]*)[\"\']?[^>]*>([^<]*)</a>'im",$body,$matches);

you could then use parse_url() to get your variables, and then use preg_split to seperate teh variables.

AbraCadaver

6:56 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Thanks! I have already gotten the anchor and extracted the url from the href. What I'm trying to do is modify the url with preg_replace (if this is best?).

Here is what I have now, but I was hoping to combine the regexes and replacements from the array elements into to 1 regex.

$search = array(
"/(script).php\?((\w+)=(\w+))/",
"/&((\w+)=(\w+))*$/",
"/&((\w+)=(\w+))*/",
);

$replace = array(
"$1-$3-$4",
"-$2-$3.html",
"-$2-$3",
);

$newhrefs = preg_replace($search, $replace, $hrefs);

Thanks!
Shawn

AbraCadaver

7:05 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Maybe I'm over complicating it because I am regex challenged, but here's the short of it:

replace: /(script).php\?((\w+)=(\w+))/
with: $1-$3-$4

and replace any: /&((\w+)=(\w+))*/
with: -$2-$3

and if any of the previous ended the line:
append: .html

Thanks!
Shawn

andreasfriedrich

7:34 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




$test = '<a href="script.php?name=aaron&age=15">Aaron</a>';
#
echo preg_replace("'script.php\?([^\"]+)'e",
"'script-'.implode('-', preg_split('/&¦=/', '\\1')).'.html'",
$test);

will echo() [php.net]

script-name-aaron-age-15.html
.

BOL´s idea pointed into the right direction. Doing the matching for any given number of parameter using a single regular expression would be quite hard. That is why I choose to extract the entire query string using preg_replace() [php.net], "storing" it in the backreference \1 and then split it up using preg_split() [php.net]. The array returned is then implode() [php.net]d. Then 'script-' and '.html' get concatenated to the resulting string.

The replacement string is evaluated because of the e pattern modifier.

HTH Andreas


Note: The WebmasterWorld posting software deletes spaces preceding the exclamation point "!" character. It also replaces a solid vertical pipe symbol with a broken vertical pipe "¦" symbol. Both of these changes will need to be undone in any code you copy from WebmasterWorld. Make sure to include a space preceding the "!" in mod_rewrite code, and always replace "¦" with a solid vertical pipe.

AbraCadaver

7:49 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Many thanks!

My only issue is that I will need to reverse this process in a rewrite rule, and there I must use a regex.

Thanks for the replies,
Shawn

andreasfriedrich

8:16 pm on Feb 24, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>My only issue is that I will need to reverse this process
>>in a rewrite rule, and there I must use a regex.

Well you sure do need to use a regex but you do not need to do it all in one RewriteRule [httpd.apache.org] at once.


RewriteRule [httpd.apache.org] (script.*)-([^-]+)-([^-]+)\.html$ $1?$2=$3 [N,QSA]
RewriteRule [httpd.apache.org] script\.html script.php

will loop over the URL until all -parameter-value pairs are removed from the URI and added to the query string. Then script.html will be rewritten to script.php.

Andreas

AbraCadaver

8:31 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



You da man!

Thanks!

Robber

11:11 pm on Feb 24, 2003 (gmt 0)

10+ Year Member



Hmmm, now how did I know Andreas would have a little something up his sleave!

andreasfriedrich

6:09 pm on Feb 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>>Hmmm, now how did I know Andreas would have a little
>>something up his sleave!

I try very hard to have an ace up my sleeve. But this might not always be the case :o

And I had to come up with something after you started this thread with saying that this sounds like one for me ;)

Andreas

Robber

10:26 pm on Feb 26, 2003 (gmt 0)

10+ Year Member



Ahhh,

so Andreas, you like a challenge - I'll have to remeber that next time my regex is playing up :)!

Cheers