Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Substitution 1

Status
Not open for further replies.

SpitItOut

Programmer
Jul 28, 2005
54
SK
Hi, I need help. I select some text from database and use echo to output, but I need to substitute some words from this text with links from database where is assigned link to this text.

for example I want to substitute all words "microsoft" in text with <a href="
in database there are two columns - in one is "microsoft", in second is <a href="
So all words "microsoft" would be links.

any idea?

I'm using PHP5 and MySQL 4.0.20
 
sure.

just query your database like so

Code:
$result = mysql_query("select term, replacementTerm from table") or die (mysql_error());
$search = array();
$replace = array();
while ($row = mysq_fetch_array($result)){
 $search[] = $row[0];
 $replace[] = $row[1];
}
$_text = str_ireplace($search, $replace, $text);

this is case insensitive. it assumes that your text to be searched and replaced is held in a variable $text. you will, of course, have to change the query as necessary for your schema.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top