Hi, I am fairly new to PHP and wanted to impliment a feature on a PHP page and was wondering how to do it. The feature is a keyword highlighting script, and basically if a preset word is detected in a page, it will automatically make the word a link to another page. This way I don't have to link every keyword to a different page, I can just make a PHP include to do this. Any help would be appreciated and green repped. Thanks
JavaScript might be a better option to do this on the fly, unless you want to specify you content as a variable, and use str_replace or regular expressions to find instances of certain words and replace them with a predefined link. But then you would have to edit all of your existing pages to implement the changes, whereas with javascript if your content is in a div with an id specified, you can manipulate its content easily with an include file.
Agreed with above, due to the nature of PHP you'd have to store the page in a variable or similar first before you could modify it, JavaScript may be a better alternative to what you're trying to achieve. A quick Google turned up this which may be of use to you; bytes.com/forum/thread89681.html
You can do an ob_start() at the beginning of script, and at bottom use: $str= ob_get_contents(); ob_end_clean(); Now all output of script is in $str. Do an include to file which checks for words and replaces them. <?php ob_start(); //full php code $str= ob_get_contents(); ob_end_clean(); include('convert_to_links.php'); echo $str; ?> That include file could have: $str= str_replace('my_name', '<a href="my_name.php"> my_name </a>', $str); You can also use preg_replace to search and replace. regards
Great thinking, JEET! I've only played with that method once before, but I had forgotten all about it! Very nice solution.
Thanks for your input guys! JEET I am going to follow up your idea and try and make it work. I was looking for the function to change strings, so I may look up string_replace further. As for the javascript idea : I have no yet created a site, and so I don't mind how hard it is to impliment. Also I am trying to learn PHP so i'll give JavaScript a miss Thanks for all your suggestions, any more?
Sorry for double posting but there has been significant improvement I used JEET's idea, and made it work For anyone looking for a solution here it is: <?php ob_start(); ?> Hello welcome to Fitness World Any other HTML you need here! <?php $str= ob_get_contents(); ob_end_clean(); include('convert_to_links.php'); echo $str; ?> PHP: convert_to_links.php <?php $str= str_replace('Fitness', '<a href="http://love.com">Love</a>', $str); ?> PHP: This works just how I wanted it to, cheers mate Greenies all round. Good day.