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.
how to replace a word with link in a text
Moderator: General Moderators
preg_replace
Actually I just posted what you need in php's manual here.
Basically, its like this:
The garbage inside the ()'s is what says to only match out of/inside of an html tag.
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