Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    Darlington,England
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP auto hyperlink

    I'm not sure if it's possible, but what I'm looking for is the following:

    A dynamic page is generated using PHP which pulls information from a database, I need some code which will look through all text nodes in the page, find text in between "[[" and "]]" (e.g. [[text]]), check a database table for the word in between the square brackets for a link, and then surround the keyword with the hyperlink tags.

    e.g.

    1) [[text]] -> PHP page finds square brackets
    2) [[text]] -> check db for "text" keyword
    3) <a href="hyperlink">text</a>

    If anyone has any ideas and can help this would be great! I have tried a few things but it's currently over my head.

    (It must be a general piece of code so that ALL text nodes on the page are read).

  • #2
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    PHP Code:
    function find_text($str) {
        
    preg_match_all("#\[\[(((?!\]\]).)*)\]\]#is"$str$matches );
        if( 
    count$matches[1] ) != ) {
            return 
    $matches[1];
        }else{
            return 
    false;    
        }
    }

    if( 
    $text_found find_text("this [[is]] just a [[test]]") ) {
        foreach( 
    $text_found as $text ) {
            echo 
    $text "<br />";
        }
    }else{
        echo 
    "no text matches found.";    

    returns

    Code:
    is
    test
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • Users who have thanked Phil Jackson for this post:

    Daniel_Carmel (04-26-2010)

  • #3
    New Coder
    Join Date
    Sep 2009
    Location
    Darlington,England
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Is there anyway of applying this function to the page without providing a specific variable?

    As all entries will be contained in the database, is there anyway of screening them before they are output.

    For example my home page output is defined by the following:

    PHP Code:
    //connection include
    include ('connect.inc');

    //select database to connect to
    mysql_select_db($dbname,$conn);

    ///////////HOME//////////////////////////////////////////////////        
            
            
    case "Home":
            
                
    $homeQuery mysql_query("SELECT * FROM homecontent");
                
                
    //while there are stll page
                
    while ($home mysql_fetch_array($homeQuery))

                {
                
    $i $home['homeID'];            
                
    $img $home['image'];
                
    $imgalt$home['alt'];
                
    $content $home['textcontent'];
                
    $contentsub substr($content,0,300);
                
    $title $home['title'];
                
                
                echo 
    '
                    <div id="quart_box">
                        <table width="95%" border="0">
                            <tr>
                        <td><div id="box_link">'
    .$title.'</div></td>
                            </tr>
                            <tr>
                        <td><div id="content_pic"><img src="'
    .$img.'" alt="'.$imgalt.'" width="195" height="127"/></div></td>
                            </tr>
                            <tr>
                        <td><div class="box_content">'
    .$contentsub.'</div></td>
                            </tr>
                        </table>
                    </div>
                    '
    ;
                }
                echo 
    '
                    <div class="clear"></div>'
    ;
                break;
    ////////////////////////////////////////////////////////////////// 
    So I need it to check all the titles, and content for every piece of output before I echo it out, replace the found text, and re-input the text back into the original content and finally echo it out.

  • #4
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    im not quite getting what you want doing and how it is related to the previous question.
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #5
    New Coder
    Join Date
    Sep 2009
    Location
    Darlington,England
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If I had a sentence in the database such as:

    "Take a look at our [[about us]] section"

    I need it to find the words inside the [[ ]], which your code does nicely, then I need it to echo out a hyperlink (which will be taken from a database).

    The hard bit is putting the hyperlink back into the original content.

    So for the above sentence, it needs to return:

    "Take a look at our <a href='about_us.php'>about us</a> section"

    Hope this clears up my problem.

  • #6
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    dunno if this is much help;

    PHP Code:
    function find_text($str) {
        
    preg_match_all("#\[\[(((?!\]\]).)*)\]\]#is"$str$matches );
        if( 
    count$matches[1] ) != ) {
            return 
    $matches[1];
        }else{
            return 
    false;    
        }
    }

    $link_array = array('is' => 'http://www.google.com''test' => 'http://codingforums.com');
    $string "this [[is]] just a [[test]] [[is]]";

    if( 
    $text_found find_text$string ) ) {
        foreach( 
    $text_found as $text ) {
            
    $string str_replace("[[" $text "]]""<a href=\"" $link_array[$text] . "\">" $text "</a>"$string);
        }
        echo 
    $string;
    }else{
        echo 
    "no text matches found.";    

    returns

    Code:
    this <a href="http://www.google.com">is</a> just a <a href="http://codingforums.com">test</a> <a href="http://www.google.com">is</a>
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #7
    New Coder
    Join Date
    Sep 2009
    Location
    Darlington,England
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts
    This is perfect, thank you. I'll make a few changes and it should work exactly as I wanted! I'll post my finished code to show the changes.

  • #8
    New Coder
    Join Date
    Sep 2009
    Location
    Darlington,England
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile

    This is what I have in the end:

    PHP Code:
    //connection include
    include ('connect.inc');

    //select database to connect to
    mysql_select_db($dbname,$conn);    


    //////////////////////////////////////////////////////////////////
    ////////////////////HYPERLINKING//////////////////////////////////

    function find_text($str) {
        
    preg_match_all("#\[\[(((?!\]\]).)*)\]\]#is"$str$matches );
        if( 
    count$matches[1] ) != ) {
            return 
    $matches[1];
        }else{
            return 
    false;    
        }
    }


    //////////////////////////////////////////////////////////////////

    ///////////HOME//////////////////////////////////////////////////        
            
            
    case "Home":
            
                
    $homeQuery mysql_query("SELECT * FROM homecontent");
                
                
    //while there are stll page
                
    while ($home mysql_fetch_array($homeQuery))

                {
                
    $i $home['homeID'];            
                
    $img $home['image'];
                
    $imgalt$home['alt'];
                
    $content $home['textcontent'];
                
    $contentsub substr($content,0,300);
                
    $title $home['title'];  
                
                echo 
    '
                    <div id="quart_box">
                        <table width="95%" border="0">
                            <tr>
                        <td><div id="box_link">'
    ;
                        
                        if( 
    $text_found find_text$title ) ) {
                foreach( 
    $text_found as $text ) {
                
                
    $stringSearch mysql_query("SELECT * FROM autolink WHERE tagName = '".$text."'");
                
    $searchResult mysql_fetch_array($stringSearch);
                
    $link $searchResult['tagLink'];
                
    $string str_replace("[[".$text."]]""<a href='" .$link."'>".$text."</a>"$title);
                }
                echo 
    $string;
                   }else{
                echo 
    "no text matches found.";    
                }
                        
                        echo 
    '</div></td>
                            </tr>
                            <tr>
                        <td><div id="content_pic"><img src="'
    .$img.'" alt="'.$imgalt.'" width="195" height="127"/></div></td>
                            </tr>
                            <tr>
                        <td><div class="box_content">'
    ;
                        
                        if( 
    $text_found find_text$contentsub ) ) {
                foreach( 
    $text_found as $text ) {
                
                
    $stringSearch mysql_query("SELECT * FROM autolink WHERE tagName = '".$text."'");
                
    $searchResult mysql_fetch_array($stringSearch);
                
    $link $searchResult['tagLink'];
                
    $string str_replace("[[".$text."]]""<a href='" .$link."' id='autolink'>".$text."</a>"$contentsub);
                }
                echo 
    $string;
                   }else{
                echo 
    $contentsub;    
                }
                        
                        echo 
    '</div></td>
                            </tr>
                        </table>
                    </div>
                    '
    ;
                }
                echo 
    '
                    <div class="clear"></div>'
    ;
                break;
    ////////////////////////////////////////////////////////////////// 
    This works perfectly to change [[keywords]] into hyperlinks. It works using values from a database, putting the variables into the code using a while loop, assigns the value to a variable which then converts the value into a hyperlink.

    Finally it puts the link back into the original content using str_replace().

    Thanks for all your help, hopefully this will help other people too!

  • #9
    Senior Coder
    Join Date
    Aug 2009
    Location
    Mansfield, Nottinghamshire, UK
    Posts
    1,547
    Thanks
    57
    Thanked 148 Times in 147 Posts
    no worries
    Website Design Mansfield
    PHP Code:
    function I_LOVE(){function b(&$b='P'){$b.='P';}function a($_){return $_++;}$b='P';define("B",'H');b($b=implode('',array($b=a($b),$b=a(B))));b($b);return $b;}
    echo 
    I_LOVE(); 

  • #10
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    First of all, Thanks for a wonderful pieces of code Phil. I try to accomplish the same think as Daniel, but a little bit different. I want to find words but not inside the [[ ]], just words no brackets. Then echo out a hyperlink from the database. I try to change your code but without any sucess. Can you help me make this happen? thanks million times.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •