5

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...
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
navitronic
  • 563
  • 3
  • 15
  • Note that this regex will allow URLs like `http://./` and `http://??/`. If this is not a problem to you, you should really use `filter_var('http://www.google.com/', FILTER_VALIDATE_URL)` instead, since it’s a built-in PHP function. – Mathias Bynens Dec 03 '10 at 13:29

1 Answers1

7

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)

K Prime
  • 5,809
  • 1
  • 25
  • 19