Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Url Matching using Gruber's regex in PHP

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...
like image 491
navitronic Avatar asked Jan 08 '10 02:01

navitronic


1 Answers

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)

like image 193
K Prime Avatar answered Sep 24 '22 09:09

K Prime