0

I have input such as:

<h2 class="role">He played and an important role</h2>

And need to replace the role, but not in the class.

Tricky is, that it may be class="group role something" or so, so I essentially only want to search the real text and not the html, but I need to give back everything.

I'm in PHP and do not have a real good starting point ...

shredding
  • 5,374
  • 3
  • 46
  • 77
  • [str_replace](http://php.net/manual/en/function.str-replace.php) – chriz Feb 28 '13 at 16:20
  • A XML parser might be a better choice than regexp – jantimon Feb 28 '13 at 16:24
  • possible duplicate of [What regex pattern do I need for this?](http://stackoverflow.com/questions/1524377/what-regex-pattern-do-i-need-for-this). The approved answer is all you need. – jeroen Feb 28 '13 at 16:25
  • I don't know who `str_replace` might help here. I have tried to isolate the html but did not really figure out how to invert that. – shredding Feb 28 '13 at 16:25

2 Answers2

2

Better no preg_ for parsing HTML, use dom:

$input = '<h2 class="role">He played and an important role</h2>';

$dom = new domDocument('1.0', 'utf-8'); 
$dom->loadHTML($input); 
$dom->preserveWhiteSpace = false; 

$element = $dom->getElementsByTagName('h2'); // <--- change tag name as appropriate
$value = $element->item(0)->nodeValue;

// change $value here...
michi
  • 6,565
  • 4
  • 33
  • 56
1

It is better to use the DOM to manipulate HTML, but here is a regex solution.

It will not make the replacement if > appears before < ahead in the string.

$input = '<h2 class="role">He played and an important role</h2>';

$input = preg_replace( '/role(?![^<>]*>[^<>]*(?:<|$))/', 'new role', $input );

echo $input;    
// <h2 class="role">He played and an important new role</h2>
MikeM
  • 13,156
  • 2
  • 34
  • 47