Jump to content

Auto Hyperlink, Please Help!


vanvoquan

Recommended Posts

Hi there, I want to to find certain words and make it hyperlinks. I spent days to googled and found this piece of code. But it always find words in the brackets [[ ]]. I just want to finds certain words, without the brackets [[ ]] and make it hyperlink. So can somebody help me with this one, million thanks to you. Below is the code I found:

 

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

          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.";     
            }

Link to comment
Share on other sites

I reckon if your only trying to find

certain words, without the brackets [[ ]] and make it hyperlink
then you should use strpos and substr_replace functions then you can skip regex patterns altogether. Because if your only trying to find an occurrence of a certain word and replace that word with a hyper-link there is really no need to use regex, it would only slow you and your program down.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.