【问题标题】:Url Matching using Gruber's regex in PHP在 PHP 中使用 Gruber 的正则表达式进行 URL 匹配
【发布时间】:2010-01-08 02:34:24
【问题描述】:

我如何获得提到的正则表达式in this article 与 php 中的 preg_match 一起使用?

<?php
preg_match("\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))/i", $text, $matches);
print_r($matches);
?>

使用上面的代码我得到以下错误:

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash...

【问题讨论】:

  • 请注意,此正则表达式将允许像 http://./http://??/ 这样的 URL。如果这对您来说不是问题,您应该真正使用filter_var('http://www.google.com/', FILTER_VALIDATE_URL),因为它是一个内置的 PHP 函数。

标签: php regex url preg-match gruber


【解决方案1】:

试试这个:

preg_match("#\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))#i", $text, $matches);

您缺少正则表达式分隔符(通常是 /,但在这里使用 #,因为它更方便 URL)

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 2011-01-21
    • 1970-01-01
    • 2013-07-17
    • 2011-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多