0

I tried use preg_replace remove the div which class="image content", I use some code below, but still remain two </div> after my preg_replace, need a help, thanks.

<style>
#contract{width:100%;height:100%;}
#content{width:1002px;overflow:hidden;margin:0 auto;}
</style>
<div id="contract">
<div id="content">
<?php
$html = <<<EOT
<div style="float:left;" class="image content"><div style="float:left;"><div style="float:left;">
    <img alt="Se hai problemi nella visualizzazione dei caratteri, clicca qui." src="../../image/1001.jpg" >
</div></div></div>
<div class="content" style="float:left;"><i>some content here.</i></div>
EOT;
echo preg_replace('/<div(.*?)class="image content">([\s\S]*?)<\/div>/','',$html);
?>
</div>
</div>

I need return: <div class="content" style="float:left;"><i>some content here.</i></div>

fish man
  • 2,666
  • 21
  • 54
  • 94
  • Wrap the extraneous `()+`, or use DOMDocument (or rather the less cumbersome phpQuery/querypath/fluentdom) since your regex usage seems clumsy. (You have a few more problems there.) – mario Jul 23 '11 at 13:18
  • @mario, how to `Wrap the extraneous ()+`? `preg_replace('/
    ([\s\S]*?)(<\/div>)+/','',$html);` not work.
    – fish man Jul 23 '11 at 13:33
  • Most epic answer at stackoverflow to similar broblem... http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – petraszd Jul 23 '11 at 13:34
  • possible duplicate of [Regex Remove Images with style tag from Html](http://stackoverflow.com/questions/2772782/regex-remove-images-with-style-tag-from-html) shows how to do it with DOM. Doesnt matter if its for Images and Style tags because with DOM its the same for all elements and attributes. The abstract problem is removing element by attribute value – Gordon Jul 23 '11 at 13:38
  • *(related)* [Best Methods to parse HTML](http://stackoverflow.com/questions/3577641/best-methods-to-parse-html/3577662#3577662) – Gordon Jul 23 '11 at 13:39

1 Answers1

1

I know it's not answer to this question, but don't use regular expressions for manipulating with DOM. There are specialized classes for that (DOMDocument), which are faster and will cause you less headache.

Ondrej Slinták
  • 31,386
  • 20
  • 94
  • 126
  • I want remove all the unwilling thing in a long long text, than use `preg_split` for cut each `div` into small peaces for a `mysql insert`. so I think it is more easier use `preg_replace` for my whole code. Thanks. – fish man Jul 23 '11 at 13:17