Gossamer Forum
Quote Reply
Regex help
hello!

This is for use with PHP but the regex will be the same as perl.

Basically the REQUEST_URI will contain a key-value pair like... query=THESUBMITTEDQUERY

which could turn up straight after the ?query=THESUBMITTEDQUERY or half way through like ?name=fred&query=THESUBMITTEDQUERY&hello=world

So what I want to do is replace the "query=THESUBMITTEDQUERY" with query=$variable no matter where it sits in that sting.

$url = $_SERVER['REQUEST_URI'];
$url = preg_replace('/??????????/', "query=$variable", $url);

Can anyone help me with the right code to go in ?????????? to do that?

Thanks,

Regan
Quote Reply
Re: [ryel01] Regex help In reply to
Hi,

Mmm.. I tihnk your better option, would be to "loop" through the passed in paramaters, and then edit accordingly.

For example (I've done it in perl, as I'm afraid I don't know PHP very well ATM, as I havn't used it in years <G>)

Code:
my @array = split /&/, $ENV{QUERY_STRING};
my $loop;
foreach my $tmp (@array) {
my ($key,$val) = splity /&/, $tmp;
if ($key eq "query") {
push @loop, qq|$key=$varialbe|;
} else {
push @loop, qq|$key=$val|;
}
print join("&",@loop);
}

Sorry its in Perl - but I'm pretty busy today, so don't have time to check into the PHP formatting you would need. Hopefully this will help a bit though :)

BTW, I'm gonna move this post to the "Internet Technologies" forum, as its more suited there =)

Cheers

Andy (mod)
andy@ultranerds.co.uk


IMPORTANT: I've now moved to ultranerds.co.uk, and the .com will no longer work!
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package (plugins total "value" $3,325 & rising, for just $350)| GLinks ULTRA Package PRO (plugins total "value" $5,625 & rising, for just $500)
Support Forum | Links SQL Plugins | DMOZ Dumps | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Compare our different Plugin packages *new* Free CSS Templates
Quote Reply
Re: [Andy] Regex help In reply to
Far too much like hard work Tongue

Code:
$url = $_SERVER['REQUEST_URI'];
$url = preg_replace('/query=[^&]+/', "query=$variable", $url);
Quote Reply
Re: [Wychwood] Regex help In reply to
Awesome that worked great!

One more for you if you don't mind!?

This bit of code is out of the linksql PHP code but I need to alter it because it doesn't quite work right. I'm trying to strip out 2 different keys and their values (nh and mh) from the REQUEST_URI, and be left with $root_url. Those tags could sit anywhere in the query string ie.

Scriptname.php?someting=value&something=value&mh=4&nh=25&something=value
Scriptname.php?mh=4&nh=25

This is the existing code...

$root_url = preg_replace('/([;&?]?)(?:nh|mh)=(\d+)/e', "('\\1' == '?') ? '?' : ''", $_SERVER['REQUEST_URI'] );

Later in my script I then add those vars back into the url with different numbers later by doing...

$root_url&mh=1&nh=3

With the code above, if a script is called with a ? on the end like Script.php? it works fine. But if I leave off the ? then it creates an invalid url.

ie. Script.php&mh=1&nh=3 (missing the ?)

Any ideas how to solve this one?

Thanks

Regan