Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if a string is a URL [duplicate]

Tags:

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?

like image 201
Reham Fahmy Avatar asked Mar 08 '12 18:03

Reham Fahmy


People also ask

How do I find a duplicate URL?

To find details of specific URLs with technical duplicates, click on the blue URL Details button from the URL List. The URL Details tab will slide across, and you then need to navigate to Duplicate Content -> URLs, and you'll see all the duplicate URLs underneath.

How do I check if a string is URL?

HTMLInputElement. checkValidity() method is used to check if a string in <input> element's value attribute is URL . The checkvalidity() method returns true if the value is a proper URL and false if the input is not a proper URL.

How check string is URL or not in PHP?

This question already has answers here: $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){ // ??? }


1 Answers

The code below worked for me:

if(filter_var($text, FILTER_VALIDATE_URL))
{
    echo "Yes it is url";
    exit; // die well
}
else
{
    echo "No it is not url";
   // my else codes goes
}

You can also specify RFC compliance and other requirements on the URL using flags. See PHP Validate Filters for more details.

like image 84
user3078359 Avatar answered Oct 12 '22 06:10

user3078359