This is a link to the String in a linter.
And this is the Expression itself:
(?i)\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'\".,<>?«»“”‘’]))
I'm trying to validate almost ANY web url with this expression.
We can see here that it passes the unit tests as expected:
Yet as I said, when I try to run my code it seems to ignore validation...has me scratching my head.
These is the relevant portion of code:
//kindly taken from here: http://stackoverflow.com/a/34589895/2226328
function checkPageSpeed($url){
if (function_exists('file_get_contents')) {
$result = @file_get_contents($url);
}
if ($result == '') {
$ch = curl_init();
$timeout = 60;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER,1);//get the header
curl_setopt($ch, CURLOPT_NOBODY,1);//and *only* get the header
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//get the response as a string from curl_exec(), rather than echoing it
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,1);//don't use a cached version of the url
$result = curl_exec($ch);
curl_close($ch);
}
return $result;
}
function pingGoogle($url){
echo "<h1>".$url."</h1>";
if(strtolower(substr($url, 0, 4)) !== "http") {
echo "adding http:// to $url <br/>";
$url = "http://".$url;
echo "URL is now $url <br/>";
}
//original idea from https://gist.github.com/dperini/729294
$re = "/(?i)\\b((?:https?:\\/\\/|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}\\/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\\\".,<>?«»“”‘’]))/";
$test = preg_match($re, $url);
var_export($test);
if( $test === 1) {
echo "$url passes pattern Test...let's check if it's actually valid ...";
pingGoogle("hjm.google.cm/");
pingGoogle("gamefaqs.com");
}
else
{
echo "URL formatted proper but isn't an active URL! <br/>";
}
}