How to turn specified keywords into keywords with links?

Discussion in 'JavaScript' started by kelp, Jul 22, 2010.

  1. #1
    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
     
    kelp, Jul 22, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    But where from JavaScript will take the links of the words?
     
    s_ruben, Jul 22, 2010 IP
  3. kelp

    kelp Peon

    Messages:
    306
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    A text file or a CSV file?
     
    kelp, Jul 22, 2010 IP
  4. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    Deacalion, Jul 22, 2010 IP
  5. kelp

    kelp Peon

    Messages:
    306
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    How would I go about doing this using PHP?
     
    kelp, Jul 22, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    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):
     
    s_ruben, Jul 22, 2010 IP
  7. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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. :)
     
    Deacalion, Jul 23, 2010 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    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):
     
    s_ruben, Jul 23, 2010 IP
  9. kelp

    kelp Peon

    Messages:
    306
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    kelp, Jul 24, 2010 IP
  10. Deacalion

    Deacalion Peon

    Messages:
    438
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    1. Regular expressions, XPath or something like this.
    2. fopen() and fgetcsv()

    This would be a good excuse to learn PHP :).
     
    Deacalion, Jul 24, 2010 IP