正则去除一些DIV内的内容。
我想正则去除一些DIV的内容。div id 或者class 带comment或者share的(comment和share是一个模糊匹配。),比如
下面这段测试代码,保留Main content.去除后面的评论部分。
$htmls = <<<EOT<br /><div id="content"><br /> Main content.<br /></div><br /><div id="comment"><br /> <ul><br /> <li class="comment"><br /> <div class="header-comment"><br /> Comment:<br /> <span class="date-comment">8/11/2012, 21:25</span><br /> </div><br /> <h4 id="Some-nbsp-Text">Some Text</h4><br /> <p class="test-comment">Blah~~ Blah~~ Blah~~</p><br /> <div class="share"><br /> <div class="vote"><br /> <a class="vota yes" title="Like">2</a><br /> <a class="vota no" title="Unlike">0</a><br /> </div><br /> </div><br /> </li><br /> <li class="comment"><br /> <div class="header-comment"><br /> Comment:<br /> <span class="date-comment">8/11/2012, 23:08</span><br /> </div><br /> <h4 id="Other-nbsp-Text">Other Text</h4><br /> <p class="test-comment">Blah~~ Blah~~ Blah~~</p><br /> <div class="share"><br /> <div class="vote"><br /> <a class="vota yes" title="Like">4</a><br /> <a class="vota no" title="Unlike">0</a><br /> </div><br /> </div><br /> </li> <br /> </ul><br /></div><br />EOT;<br /><br />$htmls = preg_replace('/<div[^>]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);<br />echo $htmls;
------最佳解决方案--------------------
jordan102,有没有办法根据闭包分析并删除?
------其他解决方案--------------------
</div><br /><div id="footer"><br /> Footer content.<br /></div><br />EOT;
我是说如果后面再加一个#footer,这个footer也被去掉了。
------其他解决方案--------------------
$htmls = preg_replace('/
------其他解决方案--------------------
你可以下载一个simple_html来解析html代码
然后遍历所有的div,然后
1、获取div的id,检测是否包含comment或者share
1、获取div的class,检测是否包含comment或者share
有则删除这个div,最后输出处理后的html即可
------其他解决方案--------------------
share)[^>]*>(.*)/is', '', $htmls); //贪婪匹配
------其他解决方案--------------------
jordan大哥,你确信?这样的话,什么都返回不了了……
------其他解决方案--------------------
那就没什么好办法了。除非你能知道
------其他解决方案--------------------
我是说如果后面再加一个#footer,这个footer也被去掉了。
正则不行可以使用一个字符一个字符的检查嘛。正则有时也不是万能的,只有在有规律的字符串中才有用。

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools
