This article mainly introduces PHP to delete the HTML code in the wide-height style of the method, involving PHP for HTML code of regular matching, replacement and other operational skills, the need for friends can refer to the following
Because of the need for work, you need to collect HTML and save the HTML content to the database. To avoid impact usage, the wide-height style needs to be removed. The width, height, etc. in the case and P.
However, the collection of HTML, the style of writing each has a different, such as case, the middle of a space and so on.
As a result, the following method is written using PHP, which filters out these wonderful patterns.
The code is as follows:
<?php/** * Clear the wide height style * @param string $content contents * @return string */function clear_wh ($content) { $config = array (' Widt H ', ' 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;}? >
Demonstrate:
<?php$html = <<
Output:
Original content: <p style= "Text-align:center" width= "height=" > <p style= "width:100px" height:100 px; " > <p style= "float:left; width:100px; height:20 0 px; " ></p> </p> <p style= "width: + px; height:100px" > </p></p> filtered content: <p style=" Text-align:center "> <p Style= "" > <p style= "float:left; "></p> </p> <p style=" "> </p> </p>
Related recommendations:
Use PHP to delete the HTML code in the wide-height style
PHP is deleting the img tag method
PHP uses regular methods to remove the width-height style from the HTML code description