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

1 replies
Hi guys, below are simple script for word replacement, it is replacing a word in to link
PHP Code:
$word = array( 'google''yahoo' );  $link = array( 'google''yahoo' );   $this->post['message'] = str_ireplace($word$link$this->post['message']); 
for example text
i love google result
the output printed will be like this
HTML Code:
i love google result
it is just replacing google text into a link like this google
but the problem is if the original google text is already inside a link, like this
HTML Code:
i love google result
the output printed will be broken like this, there will be double hyperlink
HTML Code:
i love google result
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
#exclude #hyperlinks #inside #replacement
  • Profile picture of the author Jerry Jackson
    Hi Basketmen,

    You should use the strip_tags() function on your original word ($word) as follows:
    str_ireplace(strip_tags($word), $link, $this->post['message']);

    This will ensure that it's always plain text with no html tags.
    {{ DiscussionBoard.errors[3763290].message }}

Trending Topics