PHP regular expression in action: matching URLs

WBOY
Release: 2023-06-22 19:12:01
Original
2199 people have browsed it

PHP Regular Expression Practice: Matching URLs

With the popularity of the Internet, URLs have become an indispensable part of daily life. In web design, data crawling, etc., we often need to use regular expressions to match URLs. In this article, we will focus on how regular expressions in PHP can be used to match URLs.

The basic structure of a URL

First, let us understand the basic structure of a URL. Generally speaking, a URL consists of the following parts:

Protocol name: http, https, etc.
Host name: domain name or IP address
Port number: 80, 8080, etc. (optional)
Path: pointing to the specific location of the page (optional)
Query string: parameters passed during the GET request (optional)
Fragment: anchor point, pointing to the specific location within the page (optional)

A few examples:

http://www.example.com:8080/index.html?id=1#top
https://192.168.1.1/aboutUs. html
https://www.google.com/search?q=php regular expression
ftp://ftp.example.com/public/files/manual.pdf

In the above example , involving different protocols, host names, port numbers, paths, query strings and fragments.

The concept of regular expression

Regular expression is a string matching mechanism that can be used to match multiple types of information. Regular expressions usually consist of some special characters, ordinary characters, brackets and other parameters, which can be used to specify the number, position and type of characters. In PHP, we can use the preg_match() function to match regular expressions.

Build a simple regular expression

After understanding the above basic concepts, we can build a simple regular expression to match the URL. The following is a specific example:

$pattern = '/^((http|https|ftp)://)?[a-z0-9-] (.[a-z0-9-] ) ([/?#:][^s]*)?$/';
$url = 'http://www.example.com/index.html?id=1';

preg_match($pattern, $url, $matches);
print_r($matches);

In the above example, we define a regular expression $pattern to match the URL, and then use the preg_match() function to match. Finally, the matching results are printed.

Analysis code

The regular expression consists of two parts, namely: the basic structure and the matching part of each component such as protocol name, host name, path, etc. Let’s explain them one by one below.

Basic structure: '^((http|https|ftp)://)?'

  • '^' means matching the beginning of the line
  • 'http| https|ftp' means matching http, https or ftp protocol
  • '://' means matching two characters://', ​​that is, the separator between the protocol name and host name
  • ' ?' means that the previous content is optional (because some URLs may not have a protocol name)

Protocol name: '(http|https|ftp)://'

Host name: '[a-z0-9-] (.[a-z0-9-] ) '

  • '[a-z0-9-] ' means matching characters in the domain name or Numbers or '-', match at least once
  • '(.[a-z0-9-] ) ' means match the . after the domain name and the content of the domain name, match at least once

Path, query string and fragment: '([/?#:]1*)?'

  • '[/?#:]' Match various special characters in front of the path, query string or fragment, such as '/', '?', '#', ':'
  • '2' means any character except spaces, '*' means matching the previous content any number of times (that is, optional)

In summary, the above regular expression can match any type of URL. One thing to note is that in actual development, we may need to modify or expand the rules of regular expressions according to specific needs.

Summary

Regular expressions are widely used in PHP and can be used to match and verify various types of information such as characters, numbers, emails, and phone numbers. The above article introduces how to match URLs through regular expressions, and also briefly introduces the basic concepts of regular expressions. I hope readers will have a deeper understanding of the application of regular expressions.


  1. s
  2. \s

The above is the detailed content of PHP regular expression in action: matching URLs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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