Forum Moderators: coopster

Message Too Old, No Replies

Best way to replace certain words with links to pages (mySQL+PHP)

         

ruserious

8:54 am on Feb 13, 2003 (gmt 0)

10+ Year Member



I have a table in mySQL which has all the Titles of the pages (it's a wiki:-) ). They can contain any character, so some are like this "word1, word2, word3" or "word1 (word2 word3)".

Now in the forums of this site I want to hotlink the wordX to the pages in the wiki. It would be enough if it only worked for the first Word, as I assumeit would be rather "expensive" checking each and filtering out "fill words" like "in" "at" etc.

I am wondering what would be the best way.
Options I thought of:
- Make a select such, that I have an array with the word-to-be-replaced, and a link to the page. Then use str_replace (which can be used with arrays, right?). I am wondering if I can get the format I want from mySQL...otherwise I guess I'll use the preg-functions
- Select hte titels, then do a loop for matching and build the link and replace the word when I find a match.

Do you have another/better idea? Would it be wise to add another table with just the right format to do the replacing...?

dive into perl

8:13 pm on Feb 13, 2003 (gmt 0)

10+ Year Member



Hi ruserious,

You could try using the MySQL REPLACE command,
where title is the name of the field in your database.

eg :-


SELECT REPLACE(title, 'wordx', '<a href="http://wordx.com">wordx</a>') as newtitle from TABLE

Then in your PHP code print out the field newtitle.