0

I am desperatley trying to get John Grubers URL recognising regexs to work properly! The simple function I wrote always returns false, even with a blatant URL in it!

I am trying to test for any url and web specific urls in 2 different functions. I am fairly new to Regexs, so this is almost certainly me!

function:

<?php
function isweburl($url)
{
return(preg_match("(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:       [^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!    ()\[\]{};:'\".,<>?«»“”‘’]))", $url));

}

function isanyurl($url)
{
echo "suspected url:$url<br>";
return(preg_match("/(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:    [^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!    ()\[\]{};:'\".,<>?«»“”‘’]))/", $url));

}

$test=isanyurl('http://www.sega.com');
var_dump($test);
echo "<br>web test:<br>";
$test=isweburl('http://www.sega.com');
var_dump($test);
?>

Thanks in advance for any help!

  • http://stackoverflow.com/questions/2490310/regular-expression-for-checking-website-url - This should be what you are looking for. – David May 30 '12 at 21:05
  • An [identical question](http://stackoverflow.com/questions/2025095/url-matching-using-grubers-regex-in-php) – elias May 30 '12 at 21:07
  • The original expressions, I am trying to get working, are at: [link](http://http://daringfireball.net/2010/07/improved_regex_for_matching_urls) – user1427029 May 30 '12 at 22:30
  • Ah shoot, sorry Elias, you are absolutely right! Did look, but didn't find the article. – user1427029 May 31 '12 at 08:17

1 Answers1

1

Below is your regex.
I think preg_match needs a regex delimeter "/ regex /" or "~ regex ~".
But, if you use the delimeter as a character in the regex body, it has to be escaped.

"     
 (?i)\b
 (
      (?:
           https?://
        |  www\d{0,3}[.]
        |  [a-z0-9.\-]+[.][a-z]{2,4}/
      )
      (?:
           [^\s()<>]+
        |  \(
           (
                [^\s()<>]+
             |  
                (
                     \([^\s()<>]+\)
                )
           )*
           \)
      )+
      (?:
           \(
           (
                [^\s()<>]+
             |  
                (
                     \([^\s()<>]+\)
                )
           )*
           \)
        |  [^\s`!    ()\[\]{};:'\".,<>?«»“”‘’]
      )
 )
"

Since your using / as a character, try using a different delimeter.
Expanded -

"~
 (?i)\b
 (
      (?:
           https?://
        |  www\d{0,3}[.]
        |  [a-z0-9.\-]+[.][a-z]{2,4}/
      )
      (?:
           [^\s()<>]+
        |  \(
           (
                [^\s()<>]+
             |  
                (
                     \([^\s()<>]+\)
                )
           )*
           \)
      )+
      (?:
           \(
           (
                [^\s()<>]+
             |  
                (
                     \([^\s()<>]+\)
                )
           )*
           \)
        |  [^\s`!    ()\[\]{};:'\".,<>?«»“”‘’]
      )
 )
~x"