Hello, I do not have much experience with Javascript, but I would imagine it could be done using Javascript. I have keywords sprinkled throughout my website. Is there anyway to turn these keywords into links? ie. Whenever I mention Google on the page, the javascript should turn Google into Google
If you want to do this for SEO reasons - do it on the server side rather than with JavaScript. If it's more for the user experience, then go with JavaScript (look up Regular Expressions). Oh and... JavaScript can't read files, so no text or CSV database.
For example you have a text "Digital Point is very good forum and I like it." and you want to turn Digital Point to Digital Point and "forum" to forum. Here is how you can do that using PHP: <?php $text = "Digital Point is very good forum and I like it."; $words = array(); $words[0]["word"] = "Digital Point"; $words[0]["link"] = "http://forums.digitalpoint.com"; $words[1]["word"] = "forum"; $words[1]["link"] = "http://en.wikipedia.org/wiki/Internet_forum"; foreach($words as $word){ $text = preg_replace('#'.$word['word'].'(?![^<]*>)#i', '<a href="'.$word["link"].'">$0</a>', $text); } echo($text); ?> Code (markup):
There is a problem with the code posted above me... look: <?php $text = 'I am inspired by fat people jogging.'; $words = array(); $words[0]['word'] = 'red'; $words[0]['link'] = 'http://forums.digitalpoint.com'; foreach($words as $word) echo preg_replace('#'.$word['word'].'(?![^<]*>)#i', '<a href="'.$word["link"].'">$0</a>', $text); ?> PHP: Might want to add a word boundary before it.
So try this one: <?php $text = "Digital Point is very good forum and I like it."; $words = array(); $words[0]["word"] = "Digital Point"; $words[0]["link"] = "http://forums.digitalpoint.com"; $words[1]["word"] = "forum"; $words[1]["link"] = "http://en.wikipedia.org/wiki/Internet_forum"; foreach($words as $word){ $text = preg_replace('#\b'.$word['word'].'\b(?![^<]*>)#i', '<a href="'.$word["link"].'">$0</a>', $text); } echo($text); ?> Code (markup):
Two questions: 1. Right now I have a div under the class "content". Is there anyway for me to grab the all the content under the class content and export it into your script as the $text variable? 2. Is there anyway to use a text file or .CSV file with a list of keywords/link pairs that I can export into your script as the $words array? Or can you point me into the right direction? I have not dabbled in PHP very much. Thank you very much for the help guys.
1. Regular expressions, XPath or something like this. 2. fopen() and fgetcsv() This would be a good excuse to learn PHP .