我有一个简单的网址:
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
答案 0 :(得分: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);
答案 1 :(得分:0)
替换:
\?.*?\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)