what I'm trying to do is make a 'jargon buster'. Basically I have some html and some glossary terms in a database. When the person clicks on jargon buster it replaces the words in the text with a nice tooltip (wztooltip) which shows them the meanings.
I've been trying hard on this one and been looking heavily at this question Regex / DOMDocument - match and replace text not in a link
and it seems like the answer lies in the simple_html_dom libs but I'm having trouble getting it to work. Obviously any words already linked don't get touched. Here is a strip down of what I've got.
$html = str_get_html($article['content']);
$query_glossary = "SELECT word,glossary_term_id,info FROM glossary_terms WHERE status = 1 ORDER BY LENGTH(word) DESC";
$result_glossary = mysql_query_run($query_glossary);
while($glossary = mysql_fetch_array($result_glossary)) {
$glossary_link = SITEURL.'/glossary/term/'.string_to_url($glossary['word']).'-'.$glossary['glossary_term_id'];
if(strlen($glossary['info'])>400) {
$glossary_info = substr(strip_tags($glossary['info']),0,350).' ...<br /> <a href="'.$glossary_link.'">Read More</a>';
}
else {
$glossary_info = $glossary['info'];
}
$glossary_tip = 'href="javascript:;" onmouseout="UnTip();" class="article_jargon_highligher" onmouseover="'.tooltip_javascript('<a href="'.$glossary_link.'">'.$glossary['word'].'</a>',$glossary_info,400,1,0,1).'"';
$glossary_word = $glossary['word'];
$glossary_word = preg_quote($glossary_word,'/');
//once done we can replace the words with a nice tip
foreach ($html->find('text') as $element) {
if (!in_array($element->parent()->tag,array())) {
//problems are case aren't taken into account and grammer
$element->innertext = str_ireplace(''.$glossary['word'].' ',' <a '.$glossary_tip.' >'.$glossary['word'].'</a> ', $element->innertext);
//$element->innertext = str_ireplace(''.$glossary['word'].',',' <a '.$glossary_tip.'>'.$glossary['word'].'</a> ', $element->innertext);
//$element->innertext = preg_replace ("/\s(".$glossary_word.")\s/ise","nothing(' <a'.'$glossary_tip.'>'.'$1'.'</a> ')" , $element->innertext);
// $element->innertext = str_replace('__glossary_tip_replace__',$glossary_tip, $element->innertext);
}
}
}
$article['content'] = $html->save();