Home  >  Article  >  Backend Development  >  How to verify URL address format with PHP regular expression

How to verify URL address format with PHP regular expression

王林
王林Original
2023-06-24 09:51:072226browse

With the rapid development of the Internet, URL addresses have become an indispensable part of people's daily lives. In web development, in order to ensure that the URL address entered by the user can be correctly recognized and used by the system, we need to perform format verification on it. This article will introduce how to use PHP regular expressions to verify URL address format.

1. The basic components of the URL address

Before understanding how to verify the URL address format, we first need to understand the basic components of the URL address. Usually, a standard URL address consists of the following parts:

1. Communication protocol part (protocol): Specifies the protocol for communication between the client and the server. Common ones include http and https protocols.

2. Host part (host): Specify the host name or IP address of the server to be connected.

3. Port number part (port): Specify the port number used by the server to be connected, which is generally a default value determined by the host name and protocol.

4. Path part (path): Specify the path to a specific page or file on the server.

5. Query string part (querystring): Additional parameters passed by the client to the server.

6. Fragment identifier part (fragment identifier): Specifies a location within the resource.

2. PHP uses regular expressions to verify the URL address format

In PHP, you can easily verify the URL address format by using the preg_match function with regular expressions.

1. Verify the communication protocol

Use the regular expression /^https?:/// to verify whether the communication protocol of the URL address is legal. Among them, ^ matches the beginning of the string, https? indicates the choice of http or https protocol, and // indicates matching the "//" character.

Sample code:

$url = "http://www.example.com";
if(preg_match('/^https?:///', $url)){
    echo "通讯协议合法";
} else {
    echo "通讯协议不合法";
}

2. Verify host name

Use regular expression /^[A-Za-z0-9.-] .[A-Za- z]{2,}$/ can verify whether the host name of the URL address is legal. Among them [A-Za-z0-9.-] means that the host name can contain letters, numbers, dashes and periods, which means matching one or more times, .[A-Za-z]{2,}$ means Matches a period followed by a string of at least two letters, and the string must be the end of the URL address.

Sample code:

$url = "http://www.example.com";
if(preg_match('/^[A-Za-z0-9.-]+.[A-Za-z]{2,}$/', parse_url($url, PHP_URL_HOST))){
    echo "主机名合法";
} else {
    echo "主机名不合法";
}

3. Verify the port number

Use the regular expression /:d $/ to verify whether the port number of the URL address is legal. Among them: means matching the colon character, d means matching one or more digits, and the number must be the end of the URL address.

Sample code:

$url = "http://www.example.com:8888";
if(preg_match('/:d+$/', parse_url($url, PHP_URL_PORT))){
    echo "端口号合法";
} else {
    echo "端口号不合法";
}

4. Verify the path

Use the regular expression /^[/w.-] $/ to verify whether the path of the URL address is legal. Among them, ^ matches the beginning of the string, [/w.-] means that the path can contain slashes, letters, numbers, underscores and periods, and matches at least one character, and $ means matches the end of the string.

Sample code:

$url = "http://www.example.com/path/to/page.html";
if(preg_match('/^[/w.-]+$/', parse_url($url, PHP_URL_PATH))){
    echo "路径合法";
} else {
    echo "路径不合法";
}

5. Verify query string

Use the regular expression /^?. $/ to verify whether the query string of the URL address is legal. Among them, ^ matches the beginning of the string, ? means matching the question mark character, . means matching at least one character, and $ means matching the end of the string.

Sample code:

$url = "http://www.example.com/path/to/page.html?param=value";
if(preg_match('/^?.+$/', parse_url($url, PHP_URL_QUERY))){
    echo "查询字符串合法";
} else {
    echo "查询字符串不合法";
}

6. Verify the complete format of the URL address

Through the above five regular expressions, we can verify whether each part of the URL address is legal one by one. But a more common approach is to combine them to verify whether the complete format of the entire URL address is legal.

For example, the following regular expression can verify whether the complete format of the URL address is legal:

/^https?://[A-Za-z0-9.-]+.[A-Za-z]{2,}(:d+)?(/[w.-]*)*(?.*)?$/i

where i means case-insensitive. This regular expression contains multiple subexpressions, which are used to verify whether the communication protocol, host name, port number, path and query string are legal.

Sample code:

$url = "http://www.example.com/path/to/page.html?param=value";
if(preg_match('/^https?://[A-Za-z0-9.-]+.[A-Za-z]{2,}(:d+)?(/[w.-]*)*(?.*)?$/i', $url)){
    echo "URL地址合法";
} else {
    echo "URL地址不合法";
}

Summary:

This article introduces how to use PHP regular expressions to verify URL address format. By verifying whether the communication protocol, host name, port number, path and query string are legal, we can quickly detect the validity of the URL address and ensure that the system can correctly use the URL address entered by the user.

The above is the detailed content of How to verify URL address format with PHP regular expression. 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