In daily PHP development, we often need to use regular expressions to match and replace strings. Among them, matching URL links is a very common requirement. This article will introduce how to use regular expressions in PHP to match various URL links.
Common URL links refer to links starting with http or https. We can use the following regular expression to match them:
$pattern = "/((http|https)://[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#!]*[w-@?^=%&/~+#])?)/i";
where i means case-insensitive. This regular expression can match the following types of URL links:
http://www.example.com http://www.example.com/index.php http://www.example.com/?id=123 https://www.example.com https://www.example.com/index.php https://www.example.com/?id=123
In addition to ordinary URL links, sometimes we also need to match IP address links. We can use the following regular expression to match them:
$pattern = "/((http|https)://)?(d{1,3}.d{1,3}.d{1,3}.d{1,3})(:d{1,5})?([w-.,@?^=%&:/~+#!]*[w-@?^=%&/~+#])?/i";
This regular expression can match the following URL links:
http://192.168.0.1 http://192.168.0.1/index.php http://192.168.0.1/?id=123 https://192.168.0.1 https://192.168.0.1/index.php https://192.168.0.1/?id=123
In some cases, we need to match URL links with www. We can use the following regular expression to match them:
$pattern = "/((http|https)://)?(www.)?[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#!]*[w-@?^=%&/~+#])?/i";
This regular expression can match the following types of URL links:
http://www.example.com http://www.example.com/index.php http://www.example.com/?id=123 https://www.example.com https://www.example.com/index.php https://www.example.com/?id=123
In some cases, we need to match non-protocol links, such as //www.example.com. We can use the following regular expression to match them:
$pattern = "/(^|[ ])([w]+?://[w]+[^ " <]*)|(^|[ ])([w]+?//[w]+[^ " <]*)|(^|[ ])(//[w]+[^ " <]*)/i";
This regular expression can match the following types of URL links:
//www.example.com //www.example.com/index.php //www.example.com/?id=123
In actual development, we may need to match various mixed links, such as links with query parameters or anchors, or links with special characters. We can use the following regular expression to match them:
$pattern = "/((http|https)://)?([a-zA-Z0-9-.]+(.[a-zA-Z]{2,3})(:d{1,5})?)([w-.,@?^=%&:/~+#]*[w-@?^=%&/~+#])?/i";
This regular expression can match the following types of URL links:
http://www.example.com/index.php?id=123#top https://www.example.com/home?name=john&age=20 https://www.example.com/1+1=2
So far, we have introduced how to use regular expressions Match various URL links. In actual development, we can choose appropriate regular expressions to complete matching as needed. At the same time, it should be noted that regular expressions have a certain impact on performance, so you need to pay attention to performance issues when processing large amounts of data.
The above is the detailed content of How to match url links using regular expression in PHP. For more information, please follow other related articles on the PHP Chinese website!