Forum Moderators: coopster

Message Too Old, No Replies

Replace expressions with links

Replace expressions with links

         

ketlane

10:00 am on Jul 14, 2005 (gmt 0)

10+ Year Member



Hi!
I have a diet site and I recently found a database that contains proteic information on many of the expressions used in my articles so I want to link every expression like this to a page that gives details on that specific expression.
Does anyone know about any solution that is already built?

Right now I extract all the words from my article and check to see if I find any of those words in my foods table. But I bet there's a better approach to this, especially since this approach does not work with expressions with multiple words.

I've seen this system work in my browser, when I had some viruses, but can't manage to find the solution when I need it.

Any ideas?

mincklerstraat

11:22 am on Jul 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can use str_replace with arrays for the stuff its supposed to find, and stuff it's supposed to replace with. Eg.,
$proteins = array('myoglobin',
'hemoglobin',
'leghemoglobin'
);
$urls = array('<a href="http://example.com/myoglobinpage.html">myoglobin</a>',
'<a href="http://example.com/hemoglobin.php">hemoglobin</a>',
'<a href="http://example.com/thisleghemoglobinstuff.asp>leghemoglobin</a>'
);
$content = str_replace($proteins, $urls, $content);

This is a bit awkward since you need to put the whole link in the replacement array. However, it uses str_replace, so it's easy to understand.

You could use regular expressions to do this more efficiently if the protein name is always in a predictable part of the url of the site - but this gets a lot more complicated, and you might not be up for that.

FromBelgium

1:14 pm on Jul 14, 2005 (gmt 0)

10+ Year Member



I have similar question. How to replace every instance of a word with a link to a page, if that page does exist (similar to Wikipedia)? To keep it simple, all url's are placed in the same folder and page name will become "word.php".