如何获得正则表达式这篇文章中提到 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...
有帮助吗?

解决方案

尝试这种情况:

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

您不翼而飞正则表达式的分隔符(通常/,但在这里使用#,因为它是针对URL更方便)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top