Regular expressions remove some content in the DIV.

Source: Internet
Author: User
Regular expressions are used to remove some content in the DIV. I want to use regular expressions to remove some DIV content. Div & nbsp; id & nbsp; or class & nbsp; with comment or share (comment and share are a fuzzy match .), For example, & lt; div & nbsp; id = "comment" & gt;, & nbsp; & lt; d regular expression removes some content in the DIV.
I want to use regular expressions to remove some DIV content. Div id or class with comment or share (comment and share are a fuzzy match .), For example.

The following test code retains Main content. removes the comments that follow.

$htmls = <<
 
     Main content.





    Comment:
    8/11/2012, 21:25

    Some Text
    Blah~~ Blah~~ Blah~~




    2
    0





    Comment:
    8/11/2012, 23:08

    Other Text
    Blah~~ Blah~~ Blah~~




    4
    0


         
         


EOT;

$htmls = preg_replace('/]*(comment|share)[^>]*>(.*?)<\/div>/is', '', $htmls);
echo $htmls;



------ Best solution --------------------
Jordan102, is there a way to analyze and delete it based on the closure?
------ Other solutions --------------------


     Footer content.

EOT;

I mean, if I add another footer, the footer is also removed.
------ Other solutions --------------------
$ Htmls = preg_replace ('/] * (comment
------ Other solutions --------------------
You can download simple_html to parse the html code.
Then traverse all the divs and
1. obtain the div id and check whether the div contains comment or share.
1. obtain the div class and check whether it contains comment or share.
If yes, delete the div and output the processed html.
------ Other solutions --------------------
Share) [^>] *> (. *) <\/div>/is ', '', $ htmls); // greedy match
------ Other solutions --------------------
Brother jordan, are you sure? In this case, nothing can be returned ......
------ Other solutions --------------------
There is no good way. Unless you know some special characters.
------ Other solutions --------------------
Reference:
PHP code12345 Footer content. EOT;
I mean, if I add another footer, the footer is also removed.

If the regular expression does not work, you can use one character or one character to check the regular expression. Regular expressions are sometimes not omnipotent. they are only useful for regular strings.

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.