Page 1 of 1

regex to replace keywords but not the word in an HTML-tag ??

Posted: Fri May 15, 2009 7:25 am
by abdz12
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:

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 );
 
 
any ideas !!!

Re: regex to replace keywords but not the word in an HTML-tag ??

Posted: Fri May 15, 2009 7:30 am
by prometheuzz
To me, it is not clear what you want. What word "only"? I don't see any substring "only" in your code.

Re: regex to replace keywords but not the word in an HTML-tag ??

Posted: Fri May 15, 2009 10:16 am
by abdz12
sorry i mean by only ( keywords outside html tags ) , and in my code above i assumed "test" as my keyword

Re: regex to replace keywords but not the word in an HTML-tag ??

Posted: Fri May 15, 2009 10:55 am
by prometheuzz
abdz12 wrote:sorry i mean by only ( keywords outside html tags ) , and in my code above i assumed "test" as my keyword
Can you provide an actual example including the desired output?
Now all you did is post some example input without telling what you want to get back: what about these strings:

Code: Select all

"abc.test def"
"abc TeSt def"
"abc testing def"
"abc test, def"
...