I have the following script where it goes through an array of words and replaces the words with links if they match words in another array. What I need is that it only makes the links to 1 instance of the word. So if there were multiple words in the arrays, it would only make a link with the first one.
//Automatically finds words that are in the dictionary and replaces them with links in the form input
function DictEncode($forminput){
//Take form input and convert it into an array of words
$formwords = preg_replace("/[[:alnum:]|[:space:]]/","",$forminput);
$wordarray = explode(" ",$formwords);
//Connect to database and find words in the dictionary that match words in the above array
$db=mysql_connect("localhost","db","password");
mysql_select_db("mydb",$db);
$query = "SELECT * FROM dict WHERE word IN ('".implode("', '", $wordarray)."');";
$myresult = mysql_query($query);
//Go through the results of the word matching and change the word into a link and resave it as the new form input
while ($row = mysql_fetch_array($myresult)) {
$origword = " ".$row["word"];
$theword = "/".$row["word"]."/";
$worddef = $row["definition"];
$caption = "Definition: ".$origword."";
$replace = "<a href=\"javascript:void(0);\" onMouseOver=\"overlib(\'$worddef\',WIDTH,214,HEIGHT,161, BACKGROUND, \'php_news/images/def_back.gif\',PADX,20,21,PADY,30,20,TEXTSIZE,1,CAPTION,\'$caption\')\" onMouseOut=\"nd()\">$origword</a>";
$forminput = nl2br(preg_replace($theword, $replace, $forminput));
}
return $forminput;
}
//End of function