在 PHP 中使用 Gruber 的正则表达式进行 URL 匹配 [英] Url Matching using Gruber's regex in PHP

查看:35
本文介绍了在 PHP 中使用 Gruber 的正则表达式进行 URL 匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何让 中提到的正则表达式与 php 中的 preg_match 一起使用?>

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

推荐答案

试试这个:

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

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

You were missing the regex delimiters (usually /, but using # here because it's more convenient for URLs)

这篇关于在 PHP 中使用 Gruber 的正则表达式进行 URL 匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆