Page 1 of 1

how to replace a word with link in a text

Posted: Wed Aug 03, 2005 1:03 pm
by jignesh
hello guys,
i have a problem like this:
i have one paragraph text, now i want to replace a specific word from that paragraph with link , ie i want to add a link to that word.

the problem is simple, if we consider paragraph with normal text, and can be solved with str_replace, but if text contains HTML part, then i am not able to do it with str_replace,

please anyone knows equivalent regex expression which can replace text with link. i just want to replace normal text that does not contain any existing link.

preg_replace

Posted: Wed Aug 03, 2005 1:22 pm
by yakasha
Actually I just posted what you need in php's manual here.

Basically, its like this:

Code: Select all

$newstring  = preg_replace( "/match(?![^<]*?>)/", "replacement", $oldstring );  // Matches outside the html tag
$newstring2 = preg_replace( "/match(?=[^<]*?>)/", "replacement", $oldstring2 );  // Matches inside the html tag
The garbage inside the ()'s is what says to only match out of/inside of an html tag.