PHP正则表达式替换url路径

时间:2017-06-12 10:31:04

标签: php regex url

我试图替换网址路径中的单词。

F.e。 host.com/path/to/find应该成为host.com/path/to/count

但我显然没有让它发挥作用。

我尝试在字符串

的末尾添加此组:(?:/count|/find)?

并在其前面全部用$1/count替换它。

但总是当我尝试在(?:/count|/find)? - 部分之前获取部分时,我会搞砸它。

这是一个测试:

编辑:所有键代表测试(srouce)网址。并且所有值都代表预期结果。

因此,如果路径上没有“/ count”,则应添加它。

如果最后有“/ count”,那么就无所事事了。

如果路径末尾有“/ find”,则应更改为“/ count”。

如果网址中存在工作“count”某处(例如“countleaveMeAlone”),则应该(ofc)不会更改。

$urls = [

    'http://foo-bar.host.com/entity/to'                => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/'               => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/find'           => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/find/'          => 'http://foo-bar.host.com/entity/to/count/',
    'http://foo-bar.host.com/entity/to/find?some=foo'  => 'http://foo-bar.host.com/entity/to/count?some=foo',
    'http://foo-bar.host.com/entity/to/find/?foo=some' => 'http://foo-bar.host.com/entity/to/count/?foo=some',

    'http://foo-bar.host.com/entity/to/count'           => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/count/'          => 'http://foo-bar.host.com/entity/to/count/',
    'http://foo-bar.host.com/entity/to/count?some=foo'  => 'http://foo-bar.host.com/entity/to/count?some=foo',
    'http://foo-bar.host.com/entity/to/count/?foo=some' => 'http://foo-bar.host.com/entity/to/count/?foo=some',

    'http://foo-bar.host.com/entity/toleaveMeAlone'                 => 'http://foo-bar.host.com/entity/toleaveMeAlone',
    'http://foo-bar.host.com/entity/to/leaveMeAlone'                => 'http://foo-bar.host.com/entity/to/leaveMeAlone',
    'http://foo-bar.host.com/entity/to/countleaveMeAlone'           => 'http://foo-bar.host.com/entity/to/countleaveMeAlone',
    'http://foo-bar.host.com/entity/to/count/leaveMeAlone'          => 'http://foo-bar.host.com/entity/to/count/leaveMeAlone',
    'http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo'  => 'http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo',
    'http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some' => 'http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some',
];

$format = "%-70s%-70s%s\r\n";
echo sprintf($format, 'TEST', 'EXPECT', 'SUCCESS');
foreach ($urls as $url => $expect) {
    $tmp = explode('?', $url);
    $url = rtrim($tmp[0], '/');
    $query = isset($tmp[1])
        ? $tmp[1]
        : '';

    /**
     * Pattern
     * /
     *      \A                  --start string
     *      (.*)                --get all before
     *      (?:/count|/find)?   --get optional "/find" or "/count"
     *      \z                  --end string
     * /
     */
    $url = preg_replace(
        "#\A(.*)(?:/count|/find)?\z#",
        '$1/count',
        $url
    );

    $url .= strlen($query)
        ? '?' . $query
        : '';

    echo sprintf($format, $url, $expect, var_export($url === $expect, true));
}

任何帮助非常感谢

2 个答案:

答案 0 :(得分:0)

$urls = [
'http://foo-bar.host.com/entity/to'                => 'http://foo-bar.host.com/entity/to/count',
'http://foo-bar.host.com/entity/to/'               => 'http://foo-bar.host.com/entity/to/count',
'http://foo-bar.host.com/entity/to/find'           => 'http://foo-bar.host.com/entity/to/count',
'http://foo-bar.host.com/entity/to/find/'          => 'http://foo-bar.host.com/entity/to/count/',
'http://foo-bar.host.com/entity/to/find?some=foo'  => 'http://foo-bar.host.com/entity/to/count?some=foo',
'http://foo-bar.host.com/entity/to/find/?foo=some' => 'http://foo-bar.host.com/entity/to/count/?foo=some'];
foreach ($urls as $url => $expect) {   
$new_url = str_replace("find","count",$url);
echo $new_url."<br />";
}

答案 1 :(得分:0)

模式:(Pattern/Replacement Demo with Official Explanation

~^([^?]+?)/?((?:(?:/count|leaveMeAlone)(*SKIP)(*FAIL))|(?:find/??)?)((?:(?<=find)/)?)((?:\?.*)?)$~

Layman的故障:

  • ~启动模式分隔符(不是斜线,因此斜线不需要在模式中转义)
  • ^匹配字符串的开头
  • ([^?]+?)捕获一个或多个不是问号的字符(懒惰/非贪婪)
  • /?匹配零或一个斜杠
  • (捕捉......
    • (?:(?:/count|leaveMeAlone)(*SKIP)(*FAIL))如果在/count
    • 之前找到leaveMeAlone?,则取消整个字符串的资格
    • |
    • (?:find/??)?可选择使用零或一个尾部斜杠(懒惰)捕获查找
  • ) ...结束捕获
  • ((?:(?<=find)/)?)如果前面有find
  • ,则捕获零或一个斜杠
  • ((?:\?.*)?)捕获网址
  • 的可选查询字符串部分
  • $匹配字符串的结尾
  • ~结束模式分隔符

代码:(Demo

$urls = [
    'http://foo-bar.host.com/entity/to'                => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/'               => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/find'           => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/find/'          => 'http://foo-bar.host.com/entity/to/count/',
    'http://foo-bar.host.com/entity/to/find?some=foo'  => 'http://foo-bar.host.com/entity/to/count?some=foo',
    'http://foo-bar.host.com/entity/to/find/?foo=some' => 'http://foo-bar.host.com/entity/to/count/?foo=some',

    'http://foo-bar.host.com/entity/to/count'           => 'http://foo-bar.host.com/entity/to/count',
    'http://foo-bar.host.com/entity/to/count/'          => 'http://foo-bar.host.com/entity/to/count/',
    'http://foo-bar.host.com/entity/to/count?some=foo'  => 'http://foo-bar.host.com/entity/to/count?some=foo',
    'http://foo-bar.host.com/entity/to/count/?foo=some' => 'http://foo-bar.host.com/entity/to/count/?foo=some',

    'http://foo-bar.host.com/entity/toleaveMeAlone'                 => 'http://foo-bar.host.com/entity/toleaveMeAlone',
    'http://foo-bar.host.com/entity/to/leaveMeAlone'                => 'http://foo-bar.host.com/entity/to/leaveMeAlone',
    'http://foo-bar.host.com/entity/to/countleaveMeAlone'           => 'http://foo-bar.host.com/entity/to/countleaveMeAlone',
    'http://foo-bar.host.com/entity/to/count/leaveMeAlone'          => 'http://foo-bar.host.com/entity/to/count/leaveMeAlone',
    'http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo'  => 'http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo',
    'http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some' => 'http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some',
];

$pattern='~^([^?]+?)/?((?:(?:/count|leaveMeAlone)(*SKIP)(*FAIL))|(?:find/??)?)((?:(?<=find)/)?)((?:\?.*)?)$~';
$replace='$1/count$3$4';
foreach($urls as $url=>$expected){
    echo "$url\n";                                   // your input
    echo preg_replace($pattern,$replace,$url),"\n";  // my output
    echo "$expected\n\n";                            // your expected output
}

输出:

http://foo-bar.host.com/entity/to
http://foo-bar.host.com/entity/to/count
http://foo-bar.host.com/entity/to/count

http://foo-bar.host.com/entity/to/
http://foo-bar.host.com/entity/to/count
http://foo-bar.host.com/entity/to/count

http://foo-bar.host.com/entity/to/find
http://foo-bar.host.com/entity/to/count
http://foo-bar.host.com/entity/to/count

http://foo-bar.host.com/entity/to/find/
http://foo-bar.host.com/entity/to/count/
http://foo-bar.host.com/entity/to/count/

http://foo-bar.host.com/entity/to/find?some=foo
http://foo-bar.host.com/entity/to/count?some=foo
http://foo-bar.host.com/entity/to/count?some=foo

http://foo-bar.host.com/entity/to/find/?foo=some
http://foo-bar.host.com/entity/to/count/?foo=some
http://foo-bar.host.com/entity/to/count/?foo=some

http://foo-bar.host.com/entity/to/count
http://foo-bar.host.com/entity/to/count
http://foo-bar.host.com/entity/to/count

http://foo-bar.host.com/entity/to/count/
http://foo-bar.host.com/entity/to/count/
http://foo-bar.host.com/entity/to/count/

http://foo-bar.host.com/entity/to/count?some=foo
http://foo-bar.host.com/entity/to/count?some=foo
http://foo-bar.host.com/entity/to/count?some=foo

http://foo-bar.host.com/entity/to/count/?foo=some
http://foo-bar.host.com/entity/to/count/?foo=some
http://foo-bar.host.com/entity/to/count/?foo=some

http://foo-bar.host.com/entity/toleaveMeAlone
http://foo-bar.host.com/entity/toleaveMeAlone
http://foo-bar.host.com/entity/toleaveMeAlone

http://foo-bar.host.com/entity/to/leaveMeAlone
http://foo-bar.host.com/entity/to/leaveMeAlone
http://foo-bar.host.com/entity/to/leaveMeAlone

http://foo-bar.host.com/entity/to/countleaveMeAlone
http://foo-bar.host.com/entity/to/countleaveMeAlone
http://foo-bar.host.com/entity/to/countleaveMeAlone

http://foo-bar.host.com/entity/to/count/leaveMeAlone
http://foo-bar.host.com/entity/to/count/leaveMeAlone
http://foo-bar.host.com/entity/to/count/leaveMeAlone

http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo
http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo
http://foo-bar.host.com/entity/to/countleaveMeAlone?some=foo

http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some
http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some
http://foo-bar.host.com/entity/to/count/leaveMeAlone?foo=some