views:

41

answers:

3

I have a string like this:

<div class="container">
  <h3 class="hdr"> Text </h3>
  <div class="main">
    text
    <h3> text... </h3>
    ....

  </div>
</div>

how do I remove the H3 tag with the .hdr class using as little code as possible ?

+1  A: 

Using as little code as possible? Shortest code isn't necessarily best. However, if your HTML h3 tag always looks like that, this should suffice:

$html = preg_replace('#<h3 class="hdr">(.*?)</h3>#', '', $html);

Generally speaking, using regex for parsing HTML isn't a particularly good idea though.

Daniel Egeberg
thanks, that works! but why do you sau using regex is not a good idea?is it because it takes more cpu?the string I'm talking about can be quite large. it's a output buffer from a function that should write something on the screen
Alex
@Alex because HTML is not regular. Use DOM if you want to work with HTML. There is an example in the linked duplicate.
Gordon
in this case, everything besides what's inside div.main is regular :)tx
Alex
A: 

Something like this is what you're looking for...

$output = preg_replace("#<h3 class=\"hdr\">(.*?)</h3>#is", "", $input);

Use "is" at the end of the regex because it will cause it to be case insensitive which is more flexible.

Webnet
A: 

try a preg_match, then a preg_replace on the following pattern:

/(<h3
[\s]+
[^>]*?
class=[\"\'][^\"\']*?hdr[^\"\']*?[\"\']
[^>]*?>
[\s\S\d\D\w\W]*?
<\/h3>)/i

It's messy, and it should work fine only if the h3 tag doesn't have inline javascript which might contain sequences that this regular expression will react to. It is far from perfect, but in simple cases where h3 tag is used it should work.

Haven't tried it though, might need adjustments.

Another way would be to copy that function, use your copy, without the h3, if it's possible.

Alexander