PHP regular expression practice: matching website titles
In web development, we often need to extract some specific information from web pages, such as web page titles, article abstracts, etc. PHP regular expressions can help us implement these functions quickly and conveniently. This article will introduce how to use PHP regular expressions to match website titles.
1. Understand the structure of the website title
Before using PHP regular expressions to match the website title, we need to understand the structure of the website title. Usually, the website title contains the following parts:
1. Website name: It usually appears at the front of the title and indicates the name of the website.
2. Web page title: located after the website name, indicating the title of the currently opened web page.
3. Separator: It is located between the website name and the web page title. It can be a vertical bar, period and other characters, which can be used to distinguish the website name and the web page title.
For example, the following is the structure of a typical website title:
"XXXX.net | Latest news, hot information, entertainment gossip"
In this example, the website The name is "XX Network", the separator is a vertical bar "|", and the title of the web page is "Latest News, Hot Information, Entertainment Gossip".
2. Use PHP regular expressions to match the website title
After understanding the structure of the website title, we can use PHP regular expressions to match the website title.
First, we need to store the title of the website to be matched in a variable, for example:
$title = "XXXX.net | Latest news, hot information, entertainment gossip";
Then, we can use the preg_match function to match the website title. The first parameter of the preg_match function is the regular expression, the second parameter is the string to be matched, and the third parameter is an array used to store the matching results.
The following is the PHP code that matches the website title:
$title = "某某网 | 最新新闻、热点资讯、娱乐八卦"; $pattern = '/^(.*?)s*|s*(.*?)$/'; if (preg_match($pattern, $title, $matches)) { $site_name = $matches[1]; $page_title = $matches[2]; echo "网站名称:".$site_name." "; echo "网页标题:".$page_title." "; }
In this code, we use the regular expression "/^(.?)s|s(.?)$/" to match the website title. The meaning of this regular expression is as follows:
1.^(.?): means matching the website name, using .? means matching as few characters as possible, ? means non-greedy matching .
2.s|s: means matching the delimiter, s* means matching any number of blank characters, | means matching the vertical bar.
3.(.?): Indicates matching the title of the web page, using .? means matching as few characters as possible.
4.$: Indicates the end of the matching string.
If the regular expression successfully matches the $title string, the $matches array will contain the matching results. $matches[1] represents the website name, $matches[2] represents the webpage title.
Finally, we output the matching results:
Website name: XX.net
Webpage title: Latest news, hot information, entertainment gossip
三, Conclusion
This article introduces how to use PHP regular expressions to match website titles. In actual development, we can adjust the regular expression pattern according to our own needs to adapt to different types of website titles. At the same time, we can also use other functions of PHP to extract other information from the web page, such as article summary, author name, etc.
The above is the detailed content of PHP regular expression in action: matching website title. For more information, please follow other related articles on the PHP Chinese website!