Hongmu Notes
Home Language Notes Remove all <div> tags from HTML code using PHP.
Language Notes PHP PHP collection

Remove all <div> tags from HTML code using PHP.

Remove all <div> tags from HTML code using PHP.

You can use PHP's built-in functions. strip_tags() Use a regular expression to remove `div` tags from HTML code. Here is an example code:

$html = '<div>这是一个 div 标签</div><p>这是一个 p 标签</p>';

// 去除所有 div 标签及其内容
$html = preg_replace('/<div\b[^>]*>(.*?)<\/div>/is', '', $html);

// 去除所有标签,只保留文本内容
$html = strip_tags($html);

echo $html; // 输出:这是一个 p 标签

In this example, it is used first. preg_replace() Functions and Regular Expressions /<div\b[^>]*>(.*?)<\/div>/is Removes all <div> tags and their content. This regular expression matches: <div> Replace the tags and their content with an empty string.

Then, use it. strip_tags() The function removes all tags, retaining only the text content. The final output is the text content with the <div> tags removed.

Please note that this regular expression does not fully match all possible `div` tags; for example, it may not correctly handle `div` tags with attributes. If your HTML code contains more complex `div` tag structures, you may need to use a more sophisticated regular expression to match and remove these tags.

微信赞赏

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

(Adaptive mobile version) English-language foreign trade ballet shoe website – PBootCMS template; Latin dance shoe website source code download – 0644

(Adaptive mobile version) English-language foreign trade ballet shoe website – PBootCMS template; Latin dance shoe website source code download – 0644 Practical Collection pbootcms Template

An English-language PbootCMS website template for export-oriented ballet and Latin dance shoes, compatible with both PC and WAP devices. Featuring a fashionable, international design style, this template is ideal for dance shoe manufacturers to showcase their product ranges and export advantages. It helps Chinese footwear manufacturers promote their brands in overseas markets and secure international orders. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4...
👁 38
Responsive Precision Instrument Manufacturing Website Template 0293

Responsive Precision Instrument Manufacturing Website Template 0293 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the precision instrument manufacturing industry. Its professional and sophisticated design enables effective presentation of instrument products, manufacturing processes, technical specifications, and scientific research applications. It helps precision instrument manufacturers showcase their products online and attract both research and industrial clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 48
Government Association Website Template 1141

Government Association Website Template 1141 Practical Collection Yiyou template

An eYouCMS website template designed specifically for government agencies and industry associations. Its sophisticated and professional design is ideal for showcasing government transparency initiatives, association functions, industry trends, and member services. This template helps government bodies and industry associations establish an official online information publishing platform. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for eYouCMS | eYouCMS...
👁 38
View the fireworks source code

View the fireworks source code Practical Collection Website source code

🔒 Hidden content: View the link after commenting: https://pan.baidu.com/s/1G3eLaecjq34uvbRiU-K1GA?pwd=9257 – Extraction code: 9257. Copy this link and open the Baidu Netdisk mobile app to access the file; the process is very convenient. The code is simple – just upload and extract the file to run it and view the result.
👁 281
(Adaptive Mobile Version) pbootCMS responsive digital electronics website template – Download HTML5 smart speaker device website source code: 0645

(Adaptive Mobile Version) pbootCMS responsive digital electronics website template – Download HTML5 smart speaker device website source code: 0645 Practical Collection pbootcms Template

A responsive digital product and smart speaker website template compatible with both PC and WAP devices. Featuring a modern, tech-driven design style that showcases the functionality and aesthetic appeal of digital products, this template helps digital brands attract tech enthusiasts online and boost product sales. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 52
Home Textile Knitted Bedding Website Template 0294

Home Textile Knitted Bedding Website Template 0294 Practical Collection Yiyou template

An EyouCMS website template designed specifically for the home textiles, knitwear, and bedding industry. Its warm and cozy design style is ideal for showcasing home textile products, bedding collections, brand stories, and home decor combinations. This template helps home textile brands showcase their products online and attract domestic consumers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | Eyou...
👁 37