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
https://forums.phpfreaks.com/topic/220803-auto-hyperlink-please-help/
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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.