[How to] Exclude replacement inside hyperlinks, maybe using if statement,or other way

Hi guys, below are simple script for word replacement, it is replacing a word in to link

$word = array(
'google',
'yahoo'
);

$link = array(
'<a href="http://google.com">google</a>',
'<a href="http://yahoo.com">yahoo</a>'
);


$this->post['message'] = str_ireplace($word, $link, $this->post['message']);

for example text

[QUOTE]i love google result

the output printed will be like this

i love <a href="http://google.com">google</a> result

it is just replacing google text into a link like this <a href=“http://google.com”>google</a>[/QUOTE]

but the problem is if the original google text is already inside a link, like this

<a href="http://google.com">i love google result</a>

the output printed will be broken like this, there will be double hyperlink

<a href="http://google.com">i love <a href="http://google.com">google</a> result</a>

please help guys, how to exclude the text google inside hyperlinks above from replaced, but the text google outside hyperlinks are still replaced

maybe using if statement in the simple script above, or other way

Can you just strip all the tags first, or at least all the <a> tags?

walk the string as a DOMDocument ([FPHP]domdocument.loadhtml[/FPHP]) instead, and skip A tags.

i dont know how to do it from above simple script, can you guys give example please?