Note that there are some explanatory texts on larger screens.

plurals
  1. POSearch and Replace Words in HTML
    text
    copied!<p>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.</p> <p>I've been trying hard on this one and been looking heavily at this question <a href="https://stackoverflow.com/questions/4044812/regex-domdocument-match-and-replace-text-not-in-a-link">Regex / DOMDocument - match and replace text not in a link</a></p> <p>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.</p> <pre><code>$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'])&gt;400) { $glossary_info = substr(strip_tags($glossary['info']),0,350).' ...&lt;br /&gt; &lt;a href="'.$glossary_link.'"&gt;Read More&lt;/a&gt;'; } else { $glossary_info = $glossary['info']; } $glossary_tip = 'href="javascript:;" onmouseout="UnTip();" class="article_jargon_highligher" onmouseover="'.tooltip_javascript('&lt;a href="'.$glossary_link.'"&gt;'.$glossary['word'].'&lt;/a&gt;',$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-&gt;find('text') as $element) { if (!in_array($element-&gt;parent()-&gt;tag,array())) { //problems are case aren't taken into account and grammer $element-&gt;innertext = str_ireplace(''.$glossary['word'].' ',' &lt;a '.$glossary_tip.' &gt;'.$glossary['word'].'&lt;/a&gt; ', $element-&gt;innertext); //$element-&gt;innertext = str_ireplace(''.$glossary['word'].',',' &lt;a '.$glossary_tip.'&gt;'.$glossary['word'].'&lt;/a&gt; ', $element-&gt;innertext); //$element-&gt;innertext = preg_replace ("/\s(".$glossary_word.")\s/ise","nothing(' &lt;a'.'$glossary_tip.'&gt;'.'$1'.'&lt;/a&gt; ')" , $element-&gt;innertext); // $element-&gt;innertext = str_replace('__glossary_tip_replace__',$glossary_tip, $element-&gt;innertext); } } } $article['content'] = $html-&gt;save(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload