vanvoquan Posted December 6, 2010 Share Posted December 6, 2010 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."; } Quote Link to comment https://forums.phpfreaks.com/topic/220803-auto-hyperlink-please-help/ Share on other sites More sharing options...
saer Posted December 11, 2010 Share Posted December 11, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/220803-auto-hyperlink-please-help/#findComment-1145937 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.