Php 如何使用preg_replace()从URL中删除重复值
我有一个简单的URL:Php 如何使用preg_replace()从URL中删除重复值,php,regex,preg-replace,Php,Regex,Preg Replace,我有一个简单的URL: http://sample.com/?page=1&page=2 http://sample.com/?page=2&page=1 如何使用preg_replace()删除重复值(第页) 代码如下: $link = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $link ); 但是结果是错误的,我如何修复它,使结果看起来像: http://sample.com/?page=1&page=2 => ht
http://sample.com/?page=1&page=2
http://sample.com/?page=2&page=1
如何使用preg_replace()删除重复值(第页)
代码如下:
$link = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $link );
但是结果是错误的,我如何修复它,使结果看起来像:
http://sample.com/?page=1&page=2 => http://sample.com/?page=2
http://sample.com/?page=2&page=1 => http://sample.com/?page=1
您可以考虑递归子模式,而不是尝试使用后备引用。
$link = 'http://sample.com/?page=1&page=2';
$link = preg_replace('/\b(\w+=\d+)&((?1))/', '$2', $link);
echo $link; //=> "http://sample.com/?page=2"
使用反向引用,我想您可以:
$link = preg_replace('/\b(\w+=)\d+&(\1\d+)/', '$2', $link);
替换:
\?.*?\K([^=]+)(?:=[^&]*)?&(.*)\1=(.*?)(?:&|$)
与:
\2\1=\3
\? (?# match ? literally)
.*? (?# lazily match unnecessary params)
\K (?# throw away everything to the left)
([^=]+) (?# capture 1+ non-= characters)
(?: (?# start non-capturing group)
= (?# match = literally)
[^&]* (?# match 0+ non-& characters)
)? (?# end optional parameter value)
& (?# match & literally)
(.*) (?# capture optional inbetween junk)
\1 (?# a paramater that has already been seen in capture group 1)
= (?# literally match =)
(.*?) (?# lazily capture the newest value)
(?: (?# begin non-capturing group)
& (?# literally match &)
| (?# OR)
$ (?# match the end of the URL)
) (?# end non-capturing group)