Home  >  Article  >  Backend Development  >  How to determine if php is a url

How to determine if php is a url

藏色散人
藏色散人Original
2020-08-20 10:16:2210534browse

How to use PHP to determine whether it is a URL: 1. Use the regular expression "preg_match($preg,$url)" to achieve the determination; 2. Use the PHP built-in function "filter_var ()" to achieve the determination.

How to determine if php is a url

Recommended: "PHP Video Tutorial"

PHP determines whether the legal string of the URL is URL link

There are many friends who cannot accurately determine whether a string is a standard URL link address when crawling or under other circumstances, which is very troublesome. Let's take a look at how to determine the URL. of legality.

The following codes are all PHP language codes, but the languages ​​​​are all the same.

The first type: regular expression

<?php
function or_url($url){
    $preg = "/http[s]?:\/\/[\w.]+[\w\/]*[\w.]*\??[\w=&\+\%]*/is";
    if(preg_match($preg,$url)){
        echo &#39;正确的 url 地址&#39;;
    }else{
        echo &#39;不是合法的 url 地址&#39;;
    }
}

The second type: using the built-in function filter_var () recommended

<?php
function or_url($url){
    if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
        echo &#39;url 地址正确&#39;;
    }else{
        echo &#39;url 地址不正确&#39;;
    }
}

The above is the detailed content of How to determine if php is a url. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn