PHP Regular Expression deletes the width and height styles in HTML code, and phphtml code styles.
This article describes how to delete the width and height styles in HTML code using PHP regular expressions. We will share this with you for your reference. The details are as follows:
For work purposes, you need to collect html and save the html content to the database. To avoid impact on use, you must delete the width and height styles. Width and height in the sample slice and div.
However, in the collected html, styles are written differently, such as case and space in the middle.
Therefore, the following method is compiled using php regular expressions to filter out these wonderful styles.
The Code is as follows:
<? Php/*** clear wide and high styles * @ 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 <div style = "text-align: center" width = "500" height = "300"> <div style = "Width: 100px; height: 100 px; "> <div style =" float: left; width: 100px; height: 200 px; "> </div> <div style =" width: 100 px; height: 100px "> </div> HTML; echo '<xmp> '; echo 'original content :'. PHP_EOL; echo $ html. P HP_EOL.PHP_EOL; echo 'filtered content: '. PHP_EOL; echo clear_wh ($ html); echo' </xmp> ';?>
Output:
Original content: <div style = "text-align: center" width = "500" height = "300"> <div style = "Width: 100px; Height: 100 px; "> <div style =" float: left; width: 100px; height: 200 px; "> </div> <div style =" width: 100 px; height: 100px "> </div>: <div style = "text-align: center"> <div style = ""> <div style = "float: left; "> </div> <div style =" "> </div>
PS: here we will provide two very convenient Regular Expression tools for your reference:
JavaScript Regular Expression online testing tool:
Http://tools.jb51.net/regex/javascript
Regular Expression generation tool:
Http://tools.jb51.net/regex/create_reg