这些正则表达式模式匹配什么?

发布于 2024-07-20 08:15:16 字数 383 浏览 3 评论 0原文

我是 PHP 中正则表达式的新手,了解基本模式,但是下面的模式有点复杂,我不明白以下模式匹配什么:

$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#... "<a href='' rel='nofollow'></a>", $ret);

$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*... "<a href='http://' rel='nofollow'></a>", $ret);

有人可以解释一下吗?

谢谢。

I am new to regex in PHP and understand the basic patterns however the ones below are a bit complex and I don't understand what the following pattern matches:

$ret = preg_replace("#(^|[\n ])([\w]+?://[\w\#$%&~/.\-;:=,?@\[\]+]*)#... "<a href='' rel='nofollow'></a>", $ret);

$ret = preg_replace("#(^|[\n ])((www|ftp)\.[\w\#$%&~/.\-;:=,?@\[\]+]*... "<a href='http://' rel='nofollow'></a>", $ret);

Could someone please explain them?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

远昼 2024-07-27 08:15:16

简而言之:用链接替换 ​​URL。

详细信息:

  1. 第一个正则表达式描述以单词字符 ([\w]+) 开头、后跟 :// 和一个或多个字符的序列集合[\w\#$%&~/.\-;:=,?@\[\]+]

    这可能应该与以 URL 协议/方案开头的 URL 匹配,例如 http://https://ftp://.

    但它也会匹配 javascript://。 这不太好: javascript://%0Aalert%28%22booo%21%22%29 等于 JavaScript 代码:

    <前><代码>//
    警报(“嘘!”)

  2. 第二个正则表达式描述以 www.ftp.,同样后跟一组 [\w\#$%&~/.\-;:=,?@\[ 中的一个或多个字符\]+]

    这可能应该与以 www.ftp. 开头的 URL 匹配。 然后将 URL 协议/方案添加到 URL 中。

In short: Replace URLs by links.

In detail:

  1. The first regex describes sequences that begin with word characters ([\w]+), followed by ://, followed by one or more characters of the set [\w\#$%&~/.\-;:=,?@\[\]+].

    That should probably match a URL beginning with the URL protocol/scheme like http://, https:// or ftp://.

    But it would also match javascript://. And that’s not good: javascript://%0Aalert%28%22booo%21%22%29 equals the JavaScript code:

    //
    alert("booo!")
    
  2. The second regex describes sequences that begin with either www. or ftp., again followed by one or more characters of the set [\w\#$%&~/.\-;:=,?@\[\]+].

    That should probably match URLs, that just begin with www. or ftp.. The URL protocol/scheme is then added to the URL.

初见你 2024-07-27 08:15:16

获取 RegexBuddy,它会向您解释 (查看屏幕截图)任何正则表达式的含义。 SO 中还有另一个 anwser 演示了这一点

无论如何,根据 preg_replace 的第二个参数,它们应该匹配 URL 并对其进行标记。

Get RegexBuddy, and it explains you (see screenshots) what any regular expression means. There is another anwser here in SO that demonstrates that.

Anyway, according to the second arguments of the preg_replaces, they should match URLs and tagify them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文