This question already has an answer here:
- Best way to check if a URL is valid 8 answers
I've seen many questions but wasn't able to understand how it works as I want a more simple case.
If we have text, whatever it is, I'd like to check if it is a URL or not.
$text = "something.com"; //this is a url
if (!IsUrl($text)){
echo "No it is not url";
exit; // die well
}else{
echo "Yes it is url";
// my else codes goes
}
function IsUrl($url){
// ???
}
Is there any other way rather than checking with JavaScript in the case JS is blocked?
You could use the following regex pattern to check if your variable is an url or not :
I don't think there is a definitive answer to this. Example of a valid URL:
If you have some text. and not a large amount of it. You can check by doing a CURL request and see if that returns a valid response. Otherwise if I put localhost, it could be a link and it could be something else and you wouldn't be able check it.