Home > Backend Development > PHP Tutorial > How to verify the legitimacy of a URL using PHP regular expressions

How to verify the legitimacy of a URL using PHP regular expressions

PHPz
Release: 2023-06-24 10:00:01
Original
1626 people have browsed it

In modern web development, URL is already a very important concept. In our websites and web applications, URL can be used to identify the location of resources, and in many cases it is necessary to detect illegal URLs. for processing. This article will introduce how to use PHP regular expressions to verify the legitimacy of URLs.

First, we need to understand what a URL is. URL is the abbreviation of Uniform Resource Locator (Uniform Resource Locator), which is used to uniquely identify and describe resources on the Internet. URL contains multiple components, such as protocol, host name, path, etc. The format of a complete URL is usually as follows:

protocol://hostname[:port]/path[?query][#fragment]
Copy after login

Among them, the protocol, host name and path are required parts, and the other parts are optional. Next, we will introduce how to use PHP regular expressions to check the legitimacy of URLs.

The first step is to create a regular expression to match the format of the URL. Considering that the formats of different URLs are quite different, we need to design a more relaxed regular expression. The following is an example for reference:

$pattern = '@^(https?|ftp)://[^s/$.?#].[^s]*$@i';
Copy after login

The meaning of the above regular expression is that the URL must start with a protocol , i.e. http, https or ftp, followed by the "://" symbol. Next, follow the host name. Special characters such as spaces, $, ?, #, etc. cannot exist in the host name. The path may contain any characters. Finally, there cannot be a space at the end of the entire URL, so we use s to represent a space and * to represent any character.

Next, we need to write code to use the regular expression we just created to check the validity of the URL. The following is a sample code:

function validateUrl($url) {
    $pattern = '@^(https?|ftp)://[^s/$.?#].[^s]*$@i';
    return preg_match($pattern, $url);
}
Copy after login

In the above code, the validateUrl function requires a parameter $url, which is the URL that needs to be verified. The preg_match function is used inside the function to match whether the URL matches the regular expression just created. If the match is successful, 1 is returned, otherwise 0 is returned.

Finally, when using the above code, we only need to call the validateUrl function to detect the legitimacy of the URL. The following is a test example:

$url1 = "http://www.example.com/path";
if (validateUrl($url1)) {
    echo "URL $url1 合法。
";
} else {
    echo "URL $url1 不合法。
";
}

$url2 = "http://www.example.com/path with space";
if (validateUrl($url2)) {
    echo "URL $url2 合法。
";
} else {
    echo "URL $url2 不合法。
";
}
Copy after login

In the above code, we tested two URLs: $url1 and $url2. The first URL is legal and has no special characters and spaces. The second URL contains spaces and is illegal. The program will determine whether the URL is legal based on the return value of the validateUrl function.

To sum up, this article introduces how to use PHP regular expressions to verify the legitimacy of URLs. The problem of URL validation can be solved well by creating a loose regular expression and calling the preg_match function to match whether the URL conforms to the format.

The above is the detailed content of How to verify the legitimacy of a URL using PHP regular expressions. For more information, please follow other related articles on the PHP Chinese website!

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