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

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
abdz12
Forum Newbie
Posts: 2
Joined: Fri May 15, 2009 3:50 am

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

Post 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 !!!
Last edited by Benjamin on Fri May 15, 2009 8:34 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post by prometheuzz »

To me, it is not clear what you want. What word "only"? I don't see any substring "only" in your code.
abdz12
Forum Newbie
Posts: 2
Joined: Fri May 15, 2009 3:50 am

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

Post by abdz12 »

sorry i mean by only ( keywords outside html tags ) , and in my code above i assumed "test" as my keyword
User avatar
prometheuzz
Forum Regular
Posts: 779
Joined: Fri Apr 04, 2008 5:51 am

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

Post 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"
...
Post Reply