2

我坚持使用简单的正则表达式来匹配内容中的 URL。目标是从“/folder/id/123”之类的链接中删除该文件夹,并将其替换为“id/123”,因此它是同一文件夹中的一个相对较短的文件夹。

其实我做到了

$pattern = "/\/?(\w+)\/id\/(\d)/i"
$replacement = "id/$2";
return preg_replace($pattern, $replacement, $text);

它似乎工作正常。

但是,我要进行的最后一个测试是测试每个匹配的 url 是否不包含 http://,如果它是也使用相同模式 /folder/id/123 的外部站点。

我尝试了 /[^http://] 或 (?<!html)... 和不同的东西,但没有成功。任何帮助都会非常好:-)

    $pattern = "/(?<!http)\b\/?(\w+)\/id\/(\d)/i"; ???????

谢谢 !

以下是一些示例:非常感谢您的帮助:-)

(these should be replaced, "same folder" => short relative path only)
<a href="/mysite_admin/id/414">label</a> ==> <a href="id/414">label</a>
<a href="/mYsITe_ADMIN/iD/29">label with UPPERCASE</a> ==> <a href="id/414">label with UPPERCASE</a>

(these should not be replaced, when there is http:// => external site, nothing to to)
<a href="http://mysite_admin/id/414">label</a> ==> <a href="http://mysite_admin/id/414">label</a>
<a href="http://www.google_admin.com">label</a> ==> <a href="http://www.google_admin.com">label</a>
<a href="http://anotherwebsite.com/id/32131">label</a> ==> <a href="http://anotherwebsite.com/id/32131">labelid/32131</a>
<a href="http://anotherwebsite_admin.com/id/32131">label</a> ==> <a href="http://anotherwebsite_admin.com/id/32131">label</a>
4

2 回答 2

3

不需要<用于标记look-back断言的 ,仅用/^(?!http)\/?(\w+)\/node\/(\d)/i作模式,它匹配 /foo/bar/123,但不匹配http://www.google.com/foo/bar/123

这个问题提供了一个很好的概述,可以帮助你解决这个问题

于 2012-08-13T13:31:00.963 回答
0

From the fine PHP manual - Assertions:

Note that the apparently similar pattern (?!foo)bar does not find an occurrence of "bar" that is preceded by something other than "foo"; it finds any occurrence of "bar" whatsoever, because the assertion (?!foo) is always TRUE when the next three characters are "bar". A lookbehind assertion is needed to achieve this effect.

such as:

$pattern = "/(?<!http:\/)\/(\w+)\/id\/(\d)/i";
于 2014-04-08T13:30:23.887 回答