homepage Welcome to WebmasterWorld Guest from 184.73.72.163
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Replace all words from words from db
shantzios




msg:4484841
 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




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved