PHP implementation is a way to remove the wide-height style from the HTML code

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.