5

如何让本文中提到的正则表达式与 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...
4

1 回答 1

7

试试这个:

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

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

于 2010-01-08T02:36:24.903 回答