regex to replace keywords but not the word in an HTML-tag ??
Posted: Fri May 15, 2009 7:25 am
hi all
I'm having a hard time replacing words "only", that is, do not replace occurences of the word in an HTML-tag. Here's some code: PHP Code:
any ideas !!!
I'm having a hard time replacing words "only", that is, do not replace occurences of the word in an HTML-tag. Here's some code: PHP Code:
Code: Select all
<?php
$str = "<div>text <table>
<tr>
<td valign=top> <IMG src=\"http://test.com/images/test.gif\" title=\"test\">
</tr>
</table> <B>test</B> text <a href=\"test.php\">tester</a> <A HREF=\"index.php?seek=test\" style=\"color:green\">test is tester but is not good</A> text.Test.</div>
<a href='test' >test</a>
<input title='test' />test";
$str.= '<a href="test" >test</test><img src="src" title="test" /><input title=\'test\' />test 1 test 2<b>test1</b>';
$parts = array('test');
foreach ($parts as $part ){
$str = preg_replace('#(<.*/>)?(' . $part . ')(<.*/>)?#m', '$1<span style="font-weight: bold; background-color: red;">$2</span>$3', $str);
}
var_dump( $str );