Advanced PHP script question

Discussion in 'PHP' started by jacobbannier, Nov 4, 2008.

  1. #1
    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 :)
     
    jacobbannier, Nov 4, 2008 IP
  2. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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.
     
    dev_SeeInside, Nov 4, 2008 IP
  3. iDemonix

    iDemonix Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     
    iDemonix, Nov 4, 2008 IP
  4. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #4
    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 :)
     
    JEET, Nov 4, 2008 IP
    jacobbannier likes this.
  5. dev_SeeInside

    dev_SeeInside Peon

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Great thinking, JEET! I've only played with that method once before, but I had forgotten all about it! Very nice solution.
     
    dev_SeeInside, Nov 5, 2008 IP
  6. jacobbannier

    jacobbannier Active Member

    Messages:
    1,155
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    90
    #6
    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? :)
     
    jacobbannier, Nov 5, 2008 IP
  7. jacobbannier

    jacobbannier Active Member

    Messages:
    1,155
    Likes Received:
    30
    Best Answers:
    0
    Trophy Points:
    90
    #7
    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 :D

    Greenies all round. Good day.
     
    jacobbannier, Nov 5, 2008 IP