Url Matching using Gruber's regex in PHP

偶尔善良 提交于 2019-12-10 14:23:51

问题


how do I get the regex mentioned in this article working with preg_match in php?

<?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...

回答1:


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)



来源:https://stackoverflow.com/questions/2025095/url-matching-using-grubers-regex-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!