-3

Here is some part of html in my page.

<div id="video-ad">
          <div id="wpn_ad_square">

          </div>
        </div>

I want to remove the wpn_ad_square div completely with preg_replace statement.Take note that there may be other wpn_ad_square div on page and i want to remove all of them

Here are my attempts

$html = preg_replace('#\<div\sid="wpn_ad.*?\<\/div\>#s', '', $html, 1);

$html = preg_replace('#<div id="wpn_ad.*?</div>#s', '', $html, 1);

$html = preg_replace('#<div id="wpn_ad.*?<\/div>#s', '', $html, 1);

Sadly none of them are working for me....Kindly provide a preg_replace solution so that i can know what is wrong with mine.

user2650277
  • 6,289
  • 17
  • 63
  • 132
  • 2
    http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Phil Nov 26 '13 at 04:38
  • 4
    http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php/3577662#3577662 – Surabhil Sergy Nov 26 '13 at 04:41
  • My aim here is to understand why my preg_replace is not working.I know Dom is a better alternative but i want to know what is wrong with my code – user2650277 Nov 26 '13 at 04:46
  • What is wrong is that, in 5 months, your HTML *will change* and you'll come back here asking what is wrong... – brasofilo Nov 26 '13 at 04:59

1 Answers1

3

It is very simple just the code you would like to remove with a non-greedy

$html = '
<div id="video-ad">
   <div id="wpn_ad_square">my example content</div>
</div>
';

echo preg_replace('/\<div id=\"wpn_ad_square\"\>(.*?)\<\/div\>/', '', $html);
TURTLE
  • 3,728
  • 4
  • 49
  • 50