How to determine if php is a url

藏色散人
Release: 2023-03-04 20:16:02
Original
10432 people have browsed it

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;;
    }
}
Copy after login

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;;
    }
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!