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.