Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating an inline 'Jargon' helper in PHP
    primarykey
    data
    text
    <p>I have an article formatted in HTML. It contains a whole lot of jargon words that perhaps some people wouldn't understand.</p> <p>I also have a glossary of terms (MySQL Table) with definitions which would be helpful to there people.</p> <p>I want to go through the HTML of my article and find instances of these glossary terms and replace them with some nice JavaScript which will show a 'tooltip' with a definition for the term.</p> <p>I've done this nearly, but i'm still having some problems:</p> <ul> <li>terms are being found within words (ie: APS is in Perhaps)</li> <li>I have to make sure that it doesn't do this to alt, title, linked text, etc. So only text that doesn't have any formatting applied. BUT it needs to work in tables and paragraphs.</li> </ul> <p>Here is the code I have:</p> <pre><code>$query_glossary = "SELECT word FROM glossary_terms WHERE status = 1 ORDER BY LENGTH(word) DESC"; $result_glossary = mysql_query_run($query_glossary); //reset mysql via seek so we don't have to do the query again mysql_data_seek($result_glossary,0); while($glossary = mysql_fetch_array($result_glossary)) { //once done we can replace the words with a nice tip $glossary_word = $glossary['word']; $glossary_word = preg_quote($glossary_word,'/'); $article['content'] = preg_replace_callback('/[\s]('.$glossary_word.')[\s](.*?&gt;)/i','article_checkOpenTag',$article['content'],10); } </code></pre> <p>And here is the PHP function:</p> <pre><code>function article_checkOpenTag($matches) { if (strpos($matches[0], '&lt;') === false) { return $matches[0]; } else { $query_term = "SELECT word,glossary_term_id,info FROM glossary_terms WHERE word = '".escape($matches[1])."'"; $result_term = mysql_query_run($query_term); $term = mysql_fetch_array($result_term); # CREATING A RELEVENT LINK $glossary_id = $term['glossary_term_id']; $glossary_link = SITEURL.'/glossary/term/'.string_to_url($term['word']).'-'.$term['glossary_term_id']; # SOME DESCRIPTION STUFF FOR THE TOOLTIP if(strlen($term['info'])&gt;400) { $glossary_info = substr(strip_tags($term['info']),0,350).' ...&lt;br /&gt; &lt;a href="'.$glossary_link.'"&gt;Read More&lt;/a&gt;'; } else { $glossary_info = $term['info']; } return ' &lt;a href="javascript:;" onmouseout="UnTip();" class="article_jargon_highligher" onmouseover="'.tooltip_javascript('&lt;a href="'.$glossary_link.'"&gt;'.$term['word'].'&lt;/a&gt;',$glossary_info,400,1,0,1).'"&gt;'.$matches[1].'&lt;/a&gt; '.$matches[2]; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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