Hongmu Notes
Home Language Notes Use PHP regular expressions to match and replace all <a> tags.
Language Notes PHP PHP collection

Use PHP regular expressions to match and replace all <a> tags.

Use PHP regular expressions to match and replace all <a> tags.

Here is an example code snippet that uses PHP regular expressions to match and replace all `<a>` tags:

<?php
// 假设这是要替换的 HTML 代码
$html = '<p><a href="https://example.com">Link 1</a></p><p><a href="https://example.com">Link 2</a></p>';

// 使用正则表达式进行匹配和替换
$new_html = preg_replace('/<a\s.*?>(.*?)<\/a>/', '$1', $html);

// 输出新的 HTML 代码
echo $new_html;
?>

In the above example code, we use preg_replace Use a function to replace all <a> tags. Regular expression /<a\s.*?>(.*?)<\/a>/ Matches the entire <a> tag and uses a non-greedy matching mode to extract the text within the tag. Replacement section. $1 It retains the text content from the `a` tag, thereby achieving a replacement effect.

Running the above code will output the following result:

<p>Link 1</p><p>Link 2</p>

As you can see, all <a> tags have been successfully replaced with plain text 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 Construction and Real Estate Development Company Website Template 0280

Responsive Construction and Real Estate Development Company Website Template 0280 Practical Collection Yiyou template

An eyouCMS responsive website template designed specifically for construction and real estate development companies. Its sophisticated and professional design effectively showcases construction projects, development case studies, sales information, and corporate strength. This template helps real estate developers showcase their brand online and attract potential homebuyers as well as partners. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About Installing eyouCMS...
👁 45
Responsive Advertising Design Creative Agency Website Template 1123

Responsive Advertising Design Creative Agency Website Template 1123 Practical Collection Yiyou template

An eyouCMS responsive website template designed for advertising design and creative agencies. Its innovative visual design style is ideal for showcasing advertising design works, creative case studies, brand strategy initiatives, and service offerings. This template helps advertising agencies demonstrate their professional expertise online and attract brand clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YouyouCMS...
👁 46
(Adaptive mobile version) Purple responsive electronic components website – PBootCMS template; Chip R&D and Design Website Source Code Download – 0635

(Adaptive mobile version) Purple responsive electronic components website – PBootCMS template; Chip R&D and Design Website Source Code Download – 0635 Practical Collection pbootcms Template

A purple responsive electronic component and chip R&D & Design PbootCMS website template, compatible with both PC and WAP devices. Featuring a modern and sophisticated design style, it is ideal for showcasing electronic chips, components, and R&D capabilities. This template helps semiconductor companies present their technology online and attract clients from the electronics industry. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5...
👁 37
Responsive fashion brand eyewear and accessories website template – 0281

Responsive fashion brand eyewear and accessories website template – 0281 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the eyewear and accessories industry, featuring a stylish and sophisticated design that effectively showcases eyewear products, accessory collections, brand identity, and new product launches. It helps eyewear and accessory brands attract fashion-conscious consumers online. Template Preview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (E...
👁 53
Responsive Web Design – Advertising Design Agency Website Template 1124

Responsive Web Design – Advertising Design Agency Website Template 1124 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for web design and advertising agencies. Its creative and professional design style is perfect for showcasing website designs, advertising creatives, brand visuals, and case studies. It helps design firms demonstrate their expertise online and attract corporate clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 45
EmpireCMS Special Call and Database Table

EmpireCMS Special Call and Database Table Program Notes Empire cms

The built-in thematic fields available for listing calls are: Thematic ID: [!--self.classid--]; Thematic Title: [!--pagetitle--]; Thematic Description: [!--class.intro--]; Thematic Thumbnail URL: [!--class.classimg--]; Thematic list can be queried using the Lingdong Index Tag: [e:loop={"select * fro...
👁 359