简体   繁体   English

在PHP中使用Gruber的正则表达式进行URL匹配

[英]Url Matching using Gruber's regex in PHP

how do I get the regex mentioned in this article working with preg_match in php? 如何在php中使用preg_match处理本文中提到的正则表达式?

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

Using the code above I get the following error: 使用上面的代码我收到以下错误:

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

Try this: 尝试这个:

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

You were missing the regex delimiters (usually / , but using # here because it's more convenient for URLs) 你错过了正则表达式分隔符(通常是/ ,但是在这里使用# ,因为它对URL更方便)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM