mercredi 6 mai 2015

Removing html tag with a specific class from HTML , but not the content using regular expression

In my php script, a variable has following html.

<div>
    first line starting text  <span class='highlight blink'> first line middlte text1 </span> first line end text.
    second line starting text  <span class="target"> second line middlte text2  </span> second line end text
    <div class="highlight blink"> third line text</div>
</div>

I want to remove tags with highlight class so above html looks like this (Using regex expression only)

<div>
   first line starting text  first line middlte text1 first line end text.
   second line starting text  <span class="target"> second line middlte text2  </span> second line end text
   third line text
</div>

I tried with this but it was failed to replace div tag which have multiple class (see third line, div tag must be removed)

$data = preg_replace('#<(\w+) class=["\']highlight["\']>(.*)<\/\1>#', '\2', $data);

I tried with this but it replace entire tag with classes. (See second line, span tag with target class should be stay untouched )

$data = preg_replace('#<(\w+) class=["\'](\w+)["\']>(.*)<\/\1>#', '\2', $data);

Anybody can help thanx in advance, I am trying it for 2 days

Aucun commentaire:

Enregistrer un commentaire