Welcome to WebmasterWorld Guest from 54.221.73.104

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Replace all words from words from db

     

shantzios

10:46 pm on Aug 14, 2012 (gmt 0)



Hi all,

I want to achieve following but dont know how to do so:

I have content of my page...
$nodecontent="this is test NIke CaLLaway page redirect blah blah"

I have my table in mysql which has link and url fields:
link url
nike www.nike.com
callaway www.callaway.com

I want to first look if these 2 words are in the string above and if they are present then i want to replace those words in the string with respective url.
so final output shud be:

print $nodecontent:
this is test <a href="www.nike.com">NIke</a> <a href="www.callaway.com">CaLLaway</a> page redirect blah blah"

Want to replace words in string with a tag where url is coming from database.

please help!

lostdreamer

3:10 pm on Sep 11, 2012 (gmt 0)

5+ Year Member



probably not the nicest way but here goes:

$nodecontent="this is test NIke CaLLaway page redirect blah blah"

$query = "SELECT * FROM wordlist";
$rst = mysql_query($query, $con);
while($row = mysql_fetch_assoc($rst)) {
$nodecontent= str_ireplace(" ".$row['link']." ", " <a href='". $row['url'] ."'>". $row['link'] ."</a> ");
}


This *would* (in your example) output:

this is test <a href="www.nike.com">nike</a> <a href="www.callaway.com">callaway</a> page redirect blah blah"

instead of

this is test <a href="www.nike.com">NIke</a> <a href="www.callaway.com">CaLLaway</a> page redirect blah blah"


Note the lowercase (words will be replaced with the upper/lowercase version that is in the DB.