Replace the content of a tag with a certain class

Posted by fire on Stack Overflow See other posts from Stack Overflow or by fire
Published on 2010-06-03T16:33:09Z Indexed on 2010/06/03 17:24 UTC
Read the original article Hit count: 163

Filed under:
|

I am looking for suitable replacement code that allows me replace the content inside of any HTML tag that has a certain class e.g.

$class = "blah";
$content = "new content";
$html = '<div class="blah">hello world</div>';

// code to replace, $html now looks like:
// <div class="blah">new content</div>

Bare in mind that:

  1. It wont necessarily be a div, it could be <h2 class="blah">
  2. The class can have more than one class and still needs to be replaced e.g. <div class="foo blah green">hello world</div>

I am thinking regular expressions should be able to do this, if not I am open to other suggestions such as using the DOM class (although I would rather avoid this if possible because it has to be PHP4 compatible).

© Stack Overflow or respective owner

Related posts about php

Related posts about html