


Use the regular expression method in PHP to delete the width and height styles of HTML
This article mainly introduces the method of PHP regular deletion of width and height styles in HTML code, involving PHP's regular matching, replacement and other operation skills for HTML code. Friends in need can refer to the following
Examples of this article This is a regular PHP method to delete width and height styles in HTML code. Share it with everyone for your reference, the details are as follows:
Due to work needs, it is necessary to collect html and save the html content into the database. In order to avoid affecting usage, the width and height styles need to be deleted. For example, width, height, etc. in pictures and p.
However, in the collected HTML, the styles are written in different ways, such as upper and lower case, spaces in the middle, etc.
So I wrote the following method using PHP regular to filter these weird styles.
The code is as follows:
<?php /** * 清除宽高样式 * @param String $content 内容 * @return String */ function clear_wh($content){ $config = array('width', 'height'); foreach($config as $v){ $content = preg_replace('/'.$v.'\s*=\s*\d+\s*/i', '', $content); $content = preg_replace('/'.$v.'\s*=\s*.+?["\']/i', '', $content); $content = preg_replace('/'.$v.'\s*:\s*\d+\s*px\s*;?/i', '', $content); } return $content; } ?>
Demo:
##
<?php $html = <<<HTML <p style="text-align:center" width="500" height="300"> <p style="Width : 100px ; Height: 100 px;"> <img src="/static/imghwm/default1.png" data-src="/images/test.jpg" class="lazy" style="max-width:90%"Use the regular expression method in PHP to delete the width and height styles of HTML" > <p style="float:left; width: 100px; height : 200 px;"></p> </p> <p style="width : 100 px ;height: 100px"> <img src="/static/imghwm/default1.png" data-src="/images/test.jpg" class="lazy" style="max-width:90%"Use the regular expression method in PHP to delete the width and height styles of HTML" > </p> </p> HTML; echo '<xmp>'; echo '原内容:'.PHP_EOL; echo $html.PHP_EOL.PHP_EOL; echo '过滤后内容:'.PHP_EOL; echo clear_wh($html); echo '</xmp>'; ?>Output:
原内容: <p style="text-align:center" width="500" height="300"> <p style="Width : 100px ; Height: 100 px;"> <img src="/static/imghwm/default1.png" data-src="/images/test.jpg" class="lazy" style="max-width:90%"Use the regular expression method in PHP to delete the width and height styles of HTML" > <p style="float:left; width: 100px; height : 200 px;"></p> </p> <p style="width : 100 px ;height: 100px"> <img src="/static/imghwm/default1.png" data-src="/images/test.jpg" class="lazy" style="max-width:90%"Use the regular expression method in PHP to delete the width and height styles of HTML" > </p> </p> 过滤后内容: <p style="text-align:center" > <p style=" "> <img src="/static/imghwm/default1.png" data-src="/images/test.jpg" class="lazy" alt="Use the regular expression method in PHP to delete the width and height styles of HTML" > <p style="float:left; "></p> </p> <p > <img src="/static/imghwm/default1.png" data-src="/images/test.jpg" class="lazy" alt="Use the regular expression method in PHP to delete the width and height styles of HTML" > </p> </p>
The above is the detailed content of Use the regular expression method in PHP to delete the width and height styles of HTML. For more information, please follow other related articles on the PHP Chinese website!

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Notepad++7.3.1
Easy-to-use and free code editor
