Hongmu Notes
Home Language Notes PHP uses regular expressions to match <a> links within web pages.
Language Notes PHP PHP collection

PHP uses regular expressions to match <a> links within web pages.

PHP uses regular expressions to match <a> links within web pages.

Regular expressions can be used to match hyperlinks within HTML; here is an example code:

$html = '<html><body><a href="https://www.example.com">Example</a></body></html>';

// 匹配所有的 a 标签
preg_match_all('/<a[^>]+href="([^"]*)"[^>]*>(.*)<\/a>/siU', $html, $matches);

// 输出匹配结果
print_r($matches[1]); // 输出所有链接的 URL
print_r($matches[2]); // 输出所有链接的文本内容

Explain this regular expression:

  • <aMatches the starting tag of an `a` tag.
  • [^>]+Match the attributes of an `a` tag until > Until symbol
  • href="([^"]*)"Matches the value of the `href` attribute; use. [^"]* Indicates that it can match an arbitrary number of non-quotation-mark characters.
  • [^>]*Matches other attributes of an `a` tag; use. * Indicates that zero or more non-instances can be matched. > character
  • >(.*)<\/a>Matches the end tag and text content of an `a` tag; use. .* Indicates that it can match an arbitrary number of characters; use. U The modifier indicates a greedy match, preventing the system from matching the text content of multiple <a> tags.

Please note that using regular expressions to match HTML is not a perfect solution, as HTML can have various formats and complex scenarios may occasionally arise. A better approach is to use a dedicated HTML parser—such as PHP's DOM parser or the Simple HTML DOM parser—to process HTML content.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232
PHP capture website source code and json parsing

PHP capture website source code and json parsing Language Notes PHP PHP collection

Recently, I wanted to collect the names of some resource websites, so I found a fun one when I collected it. The opposite website was loaded by ajax technology, so I found the api, so I just wrote the code directly to put it in the warehouse. The code is backed up here and will be used in the future. & lt; ? php $date = file_get_contents(' Website address ' ); $date = json_deco…
👁 136
PHP special character escaping and restoration

PHP special character escaping and restoration Language Notes PHP PHP collection PHP and mysql

Escape character is a special character constant. Escape characters are backslashed " & quot; At the beginning, followed by one or more characters. The escaped character has a specific meaning, which is different from the original meaning of the character, so it is called "escaped" character. The use of escape characters 1: turn ordinary characters into special purposes, such as back key and enter key. 2. Used to convert a character with special meaning back to its original meaning. 3. Before data is written into the database, escape characters (function …
👁 180

Recommended reading

Responsive Stem Cell Medicine Research Institute Website Template 0328

Responsive Stem Cell Medicine Research Institute Website Template 0328 Practical Collection Yiyou template

An eYouCMS responsive website template designed for Stem Cell Medicine Research Institutes. Its modern, technology-driven design is ideal for showcasing stem cell research, medical achievements, expert teams, and institutional strengths. This template helps medical research institutions establish an authoritative online presence and attract potential partners. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS...
👁 48
pbootCMS Website Builder: Beginner's Tutorial and Practical Source Code

pbootCMS Website Builder: Beginner's Tutorial and Practical Source Code Practical Collection pb tool

This video was recorded spontaneously, so there might be some parts where the explanation isn't perfect – we'd really appreciate your understanding! Download the source code for this video tutorial 🔒 Hidden content: Please leave a comment to access the link: https://pan.baidu.com/s/15kHHZY68jIv-Eun9g2UJfg?pwd=aw05 Extraction code: aw05 – Simply copy this link and open the Baidu Netdisk mobile app to access the content – it'll make things much easier!
👁 262
Cake & Bread Food Website Template 1185

Cake & Bread Food Website Template 1185 Practical Collection Yiyou template

An EyouCMS website template designed specifically for the cake and bread industry. Its cozy and appealing design is perfect for showcasing baked goods, brand stories, and franchise information. This template helps baking brands attract consumers and franchisees online while enhancing their brand awareness. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 55
(PC+WAP) Financial and Tax Accounting Promotion Leaflet – PBootCMS Website Template; Download Source Code for Financial Accounting Landing Page Websites – 0671

(PC+WAP) Financial and Tax Accounting Promotion Leaflet – PBootCMS Website Template; Download Source Code for Financial Accounting Landing Page Websites – 0671 Practical Collection pbootcms Template

A PbootCMS template designed for promotional leaflets targeting fiscal and tax bookkeeping and accounting services, compatible with both PC and WAP devices. The design features a strong marketing-oriented approach with a compact, efficient layout that effectively highlights service advantages, pricing policies, and contact information. This high-quality tool is ideal for fiscal and tax firms conducting Baidu PPC or landing page marketing campaigns, helping to significantly boost customer conversion rates. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: a...
👁 60
Wooden Decorative Materials Website Template 0329

Wooden Decorative Materials Website Template 0329 Practical Collection Yiyou template

This EyouCMS template is ideal for the wood decorative materials industry, featuring a natural and minimalist design style perfect for showcasing wood-based products, decorative applications, brand stories, and project cases. It helps wood decoration businesses showcase their products online and attract customers in the home renovation and construction sectors. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 55
Imperial Locomotive Collection Tutorial and Software

Imperial Locomotive Collection Tutorial and Software Practical Collection High-speed rail collection

Hello everyone! Today I recorded a collection tutorial for Empire CMS for everyone. I hope you will forgive me for the bad points! The publishing module of Empire CMS has been configured in the compressed package. You only need to modify it according to your own website. There is a corresponding video tutorial! Locomotive software has configured the release module! Baidu Cloud downloads it directly, and the rules inside are not deleted. You can use it as a reference! Collection usage tutorial
👁 800