CLASS web

PHP

url Validation

Validate url

Let's see how to validate a url.
Use the filter_Var() built-in function.
And use the constant FILTER_VALIDATE_URL as argument.

How to validate URLs using filter_Var()

filter_Var('URL',FILTER_VALIDATE_URL);

The first argument uses the URL address, the second uses the constant FILTER_VALIDATE_URL.
As shown in the example, you can change the purpose according to the value of a constant.
Returns true if the URL address is valid or false.
Here is an example of URL validation:

<?php
    $url = "https://www.pinkcoding.com";

    $checkUrl = filter_Var($url, FILTER_VALIDATE_URL);

    if($checkUrl == true) {
        echo "correct URL";
    } else {
        echo "not correct URL";
    }
?>

I'll check the results right below.







Result

이번에는 틀린 URL 주소를 입력해서 테스트 해봅시다.

<?php
    $url = "www.pinkcoding.com";

    $checkUrl = filter_Var($url, FILTER_VALIDATE_URL);

    if($checkUrl == true) {
        echo "correct URL";
    } else {
        echo "not correct URL";
    }
?>

I'll check the results right below.







result





ALL COMMENTS 0

Sort by

PinkCoding

PinkCoding

X

PinkCoding

Web Scratch Pad

X

loading

            
            
        

Back to the course