How to check if string has a server depending if url is absolute or relative?
function hasServer($url){
...
}
If this, returns true
hasServer('http://www.google.com/');
else return false
hasServer('/about-us/team/');
How to check if string has a server depending if url is absolute or relative?
function hasServer($url){
...
}
If this, returns true
hasServer('http://www.google.com/');
else return false
hasServer('/about-us/team/');
I think this is what you're looking for:
$url = 'http://www.google.com/';
$hasServer = filter_var($url, FILTER_VALIDATE_URL);
There are actually a few options from my experience. One would be to use the headers()
function and then to analyse what information you obtained inside the resulting array.
$arrayResult = headers('http://www.google.com/');
foreach ($arrayResult as $value)
{
echo "-- ".$value."<br>";
}
The output should give you all the information you need regarding if the url actually exists.
A simpler solution in my opinion is to just check if the fopen()
function actually works on the url!
if (fopen('http://www.google.com/', "r")
{
echo "the URL exists!<br>";
}
You can choose which one servers your needs better.