Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-21-2010, 02:04 PM   PM User | #1
Daniel_Carmel
New Coder

 
Join Date: Sep 2009
Location: Darlington,England
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Daniel_Carmel is an unknown quantity at this point
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).
Daniel_Carmel is offline   Reply With Quote
Old 04-21-2010, 02:14 PM   PM User | #2
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
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(); 
Phil Jackson is offline   Reply With Quote
Users who have thanked Phil Jackson for this post:
Daniel_Carmel (04-26-2010)
Old 04-26-2010, 11:25 AM   PM User | #3
Daniel_Carmel
New Coder

 
Join Date: Sep 2009
Location: Darlington,England
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Daniel_Carmel is an unknown quantity at this point
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.
Daniel_Carmel is offline   Reply With Quote
Old 04-26-2010, 11:49 AM   PM User | #4
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
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(); 
Phil Jackson is offline   Reply With Quote
Old 04-26-2010, 11:57 AM   PM User | #5
Daniel_Carmel
New Coder

 
Join Date: Sep 2009
Location: Darlington,England
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Daniel_Carmel is an unknown quantity at this point
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.
Daniel_Carmel is offline   Reply With Quote
Old 04-26-2010, 12:10 PM   PM User | #6
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
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(); 
Phil Jackson is offline   Reply With Quote
Old 04-26-2010, 12:34 PM   PM User | #7
Daniel_Carmel
New Coder

 
Join Date: Sep 2009
Location: Darlington,England
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Daniel_Carmel is an unknown quantity at this point
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.
Daniel_Carmel is offline   Reply With Quote
Old 04-26-2010, 03:15 PM   PM User | #8
Daniel_Carmel
New Coder

 
Join Date: Sep 2009
Location: Darlington,England
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Daniel_Carmel is an unknown quantity at this point
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!
Daniel_Carmel is offline   Reply With Quote
Old 04-26-2010, 03:24 PM   PM User | #9
Phil Jackson
Senior Coder

 
Join Date: Aug 2009
Location: Mansfield, Nottinghamshire, UK
Posts: 1,547
Thanks: 57
Thanked 148 Times in 147 Posts
Phil Jackson is on a distinguished road
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(); 
Phil Jackson is offline   Reply With Quote
Old 12-06-2010, 08:08 AM   PM User | #10
vanvoquan
New to the CF scene

 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
vanvoquan is an unknown quantity at this point
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.
vanvoquan is offline   Reply With Quote
Reply

Bookmarks

Tags
autolink, hyperlink, php, preg_replace

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:53 AM.


Advertisement
Log in to turn off these ads.