Home > Article > Backend Development > How to determine if php is a url
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.
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 '正确的 url 地址'; }else{ echo '不是合法的 url 地址'; } }
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 'url 地址正确'; }else{ echo 'url 地址不正确'; } }
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!