0

Trying to change the URL parameter value of "page" to "99" in the below string.

$pageURL = "http://www.test.com/search?loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=45&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002"

I tried this with no luck:

preg_replace("&page=.*&", "&page=99&", $pageURL);

Here is the result I want. Notice the parameter is now "&page=99&" ..etc.

$pageURL = "http://www.test.com/search?loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=99&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002"
3
  • any page number becomes 99 ? Commented Jun 5, 2015 at 1:49
  • The parameter page will have a new value of "99" as opposed to the original "45" Commented Jun 5, 2015 at 1:53
  • if its always 45 to 99 don't use regular expressions just str_replace its far more efficient str_replace("&page=45&", "&page=99&", $pageURL); Commented Jun 5, 2015 at 1:55

3 Answers 3

3

One option without regular expressions is to use parse_url with parse_str then put the URL back together.

>>> $url = parse_url("http://www.test.com/search?loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=45&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002")
=> [
   "scheme" => "http",
   "host"   => "www.test.com",
   "path"   => "/search",
   "query"  => "loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=45&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002"
   ]
>>> $query = [];
=> []
>>> parse_str($url['query'], $query)
=> null
>>> $query
=> [
   "loc"       => "Rich, VA",
   "serv"      => "1",
   "spec"      => "0",
   "min_price" => "5",
   "max_price" => "9999",
   "date"      => "",
   "time"      => "0",
   "filters"   => "0",
   "page"      => "45",
   "latPos"    => "459.16689191899521",
   "longPos"   => "-1263.08099819543457",
   "zoom"      => "11",
   "ne_lat"    => "499.31460613487112",
   "ne_lng"    => "-1222.88049770715332",
   "sw_lat"    => "458.90693340071935",
   "sw_lng"    => "-1923.2801253927002"
   ]
>>> $query['page'] = 99
=> 99
>>> $url['query'] = http_build_query($query);
=> "loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=99&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002"
>>> http_build_url($url)

For http_build_url you need the pecl-http extension. If you don't have it you can just concatenate.

>>> $url['scheme'] . '://' $url['host'] . $url ['path'] . '?' . $url['query']
=> "http://www.test.com/search?loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=99&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002"

Here is an example script

$pageURL = "http://www.test.com/search?loc=Rich%2C+VA&serv=1&spec=0&min_price=5&max_price=9999&date=&time=0&filters=0&page=45&latPos=459.16689191899521&longPos=-1263.08099819543457&zoom=11&ne_lat=499.31460613487112&ne_lng=-1222.88049770715332&sw_lat=458.90693340071935&sw_lng=-1923.2801253927002";
$newUrl = parse_url($pageUrl);
$query = parse_str($new_url['query']);
$query['page'] = 99;
$newUrl['query'] = http_build_query($query);
http_build_url($newUrl);
Sign up to request clarification or add additional context in comments.

Comments

1

You almost right:

preg_replace("/&page=\d+&/", "&page=99&", $pageURL);

1 Comment

You're welcome. If you know exactly what to replace, use str_replace instead of regular expressions.
0

try preg_replace("/&page=[^&]*/", "&page=99", $pageURL);

Your search includes the & character (. is any non newline character), which is problematic.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.