ThomasJSmart
Programmer
- Sep 16, 2002
- 634
a while ago i got lots of help from jpadie to make a dictionary script resulting in this lovely function:
it essentially goes through a large body of text adding a DIV window mouseover to all given words from the DB. thus resulting in a glossary/dictionary type script.
this seems to be giving a problem tho. It is also replacing words that are in HTML links.
Say we want to replace the word "CMS"
string:
a CMS is a lovely thing. click <a href="cms.html">here for more CMS info</a>
now all 3 of these words is being updated with a DIV mouseover when ofcourse only the first should be done...
been fiddeling with it for a while but regex is not realy my thing :/
anyone got any ideas?
Thanks!
I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
Code:
function dictionary($string){
global $db;
$result = mysql_query("SELECT * FROM cms_dictionary ORDER BY dictionary_word",$db);
while ($myrow = mysql_fetch_array($result)){
$GLOBALS['BC_DDI'] = 0;
$GLOBALS['BC_DID'] = $myrow['dictionary_id'];
$GLOBALS['BC_DIN'] = $myrow['dictionary_info'];
$GLOBALS['BC_DWO'] = $dictionary_word = $myrow['dictionary_word'];
// set up the patterns
$pattern[0] = '/(=["\'].*)('.$dictionary_word.')(.*["\'])/';
$uid = uniqid("tmp_");
$replace[0] = "$1".$uid."$3";
$pattern[1] = '/'.$dictionary_word.'/i';
//do the replacements
$tmp = preg_replace($pattern[0], $replace[0], $string); // changes all D-words that should not be changed to a tmp var.
$tmp = preg_replace_callback($pattern[1], "Dreplace", $tmp); //use callback because we need an incrementing id, changes alowable D-words to the replace.
$string = str_replace($uid, $dictionary_word, $tmp); // using str_replace as it is quicker. changes all tmp vars back to D-word.
}
return $string;
}
function Dreplace($matches){
static $di = 0; $dictionary_id = $GLOBALS['BC_DID']; $dictionary_word = $GLOBALS['BC_DWO']; $dictionary_info = $GLOBALS['BC_DIN'];
$dictionary_window = '<div class="dictionary" id="dDiv'.$dictionary_id.'_'.$di.'" name="dDiv'.$dictionary_id.'_'.$di.'" style="display:none" onMouseOver="showDictionary(\'dDiv'.$dictionary_id.'_'.$di.'\',1)" onMouseOut="showDictionary(\'dDiv'.$dictionary_id.'_'.$di.'\',0)">'.$dictionary_info.'</div>';
$dictionary_link = '<a href="javascript:void(0)" onMouseOver="showDictionary(\'dDiv'.$dictionary_id.'_'.$di.'\',1)" onMouseOut="showDictionary(\'dDiv'.$dictionary_id.'_'.$di.'\',0)">'.$dictionary_word.'</a>';
$result = $dictionary_window.$dictionary_link;
$di++;
return $result;
}
it essentially goes through a large body of text adding a DIV window mouseover to all given words from the DB. thus resulting in a glossary/dictionary type script.
this seems to be giving a problem tho. It is also replacing words that are in HTML links.
Say we want to replace the word "CMS"
string:
a CMS is a lovely thing. click <a href="cms.html">here for more CMS info</a>
now all 3 of these words is being updated with a DIV mouseover when ofcourse only the first should be done...
been fiddeling with it for a while but regex is not realy my thing :/
anyone got any ideas?
Thanks!
I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!