PHP Regular Expression deletes the width and height styles in HTML code, and phphtml code styles.

Source: Internet
Author: User
Tags php regular expression

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

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.