how to replace a word with link in a text

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

Moderator: General Moderators

Post Reply
jignesh
Forum Newbie
Posts: 4
Joined: Thu Mar 17, 2005 7:06 am

how to replace a word with link in a text

Post 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.
yakasha
Forum Newbie
Posts: 10
Joined: Wed Aug 03, 2005 1:17 pm
Location: Las Vegas, NV
Contact:

preg_replace

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