PHP更改字符串中的URL参数

时间:2015-06-05 01:48:11

标签: php regex post get

尝试更改" page"的网址参数值至" 99"在下面的字符串中。

$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"

我试了这个没有运气:

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

这是我想要的结果。请注意,参数现在是"& page = 99&" ..等

$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 个答案:

答案 0 :(得分:3)

没有正则表达式的一个选项是将parse_url与parse_str一起使用,然后将URL重新组合在一起。

>>> $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)

对于http_build_url,您需要pecl-http扩展名。如果你没有它,你可以连接。

>>> $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"

这是一个示例脚本

$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);

答案 1 :(得分:1)

你几乎是对的:

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

答案 2 :(得分:0)

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

您的搜索包括&字符(。是任何非换行符),这是有问题的。