views:

282

answers:

8

I have an URL http://test.com/test?xyz=27373&page=4&test=5 which I want to tranform by replacing the page=4 through page=XYZ

how can I do that with preg_replace?

A: 

What exactly are you trying to do? The question is a little unclear.

$XYZ = $_GET['xyz']; $PAGE = $_GET['page'];

?

A: 

Are wanting to replace each value with another, or replace both with one?

Robert DeBoer
+3  A: 

Yes, you can use

$url = preg_replace("/page=\d+/","page=XYZ",$oldurl="http://test.com/test?xyz=27373&page=4&test=5");

Or you can reconstruct the URL from $_GET superglobal.

Michael Krelin - hacker
A: 
$url = 'http://test.com/test?xyz=27373&page=4&test=5';
preg_match('/xyz=([^&]+)/', $url, $newpage);
$new = preg_replace('/page=([^&]+)/', $newpage[0], $url);
$new = preg_replace('/xyz=([^&]+)&/', '', $new);

This will turn

http://test.com/test?xyz=27373&page=4&test=5

into

http://test.com/test?page=27373&test=5

Forgive me if this isn't what you were looking to do, but your question isn't quite clear.

cpharmston
A: 

I'm sure you could do something with a regular expression. However, if the URL you've given is the one you're currently handling, you already have all the request variables in $_Request.

So, rebuild the URL, replacing the values you want to replace, and then redirect to the new URL.

Otherwise, go find a regexp tutorial.

Dave
A: 

If this is your own page (and you are currently on that page) those variables will appear in a global variable named $_GET, and you could use something like array_slice, unset or array_filter to remove the unwanted variables and regenerate the URL.

If you just have that URL as a string, then what exactly are the criteria for removing the information? Technically there's no difference between

...?xyz=27373&page=4&test=5

and

...?test=5&xyz=27373&page=4

so just removing all but the first parameter might not be what you want.

If you want to remove everything except the xyz param. Take a look at parse_url and parse_str

nickf
A: 

EDIT: There used to be informations here that were cleary wrong.

André Hoffmann
1: parse_url only works on query strings, not urls. 2: parse_url does not return queries as seperate array values, for that you need parse_str. 3: http_build_query builds queries not urls. Strangely, there is no opposite to parse_url in the default PHP library, afaik. There is one which might work in the comments though.
OIS
Not sure who gave +1 to this obviously incorrect answer.
OIS
@OIS: WHOOPS, might have been a little fast there with googling that stuff together. Yet I was so sure that there's a clean solution for this in PHP. But I might have been confusing languages. Anyway, thanks for clarifying.
André Hoffmann
+2  A: 

Do you want to set the value of xyz to the page value? I think you might need to specify a bit more. But this is easy to modify if you dont know regex.

$url = 'http://test.com/test?xyz=27373&page=4&test=5';
$urlQuery = parseUrl($url, PHP_URL_QUERY);
parse_str($urlQuery, $queryData);
$queryData['page'] = $queryData['xyz'];
unset($queryData['xyz']);
$query = http_build_query($queryData);
$outUrl = substr_replace($url, $query, strpos($url, '?'));
OIS