Ok, here's what I am working on.

I have a "comments" field that contains random text. This information is stored in a mysql table, call it COMMENTS.

I have a mysql table that contains words with descriptions, call it WORDS.

I am going to use preg_replace to locate the words in the comments fields from the COMMENTS table, then make then a hyperlink (of sort, really just a mouseover)

Now when you mouse over a word that is in the comments field that existed in the WORDS mysql table, I want to put the description from the WORDS table in a tooltip.

I can do a tooltip with pulling info from a database relatively easy (already doing it)...

Now doing that in conjunction with the preg_replace is where I am looking for assistance.

Any better way to go about it?

Thanks.

    Just a first thought off the top of my head:

    $sql = "SELECT word, description FROM words"
    $result = mysql_query($sql);
    $words = array();
    $descriptions = array();
    while($row = mysql_fetch_assoc($result))
    {
       $words[] = '/\b' . preg_quote($row['word']) . '\b/i';
       $descriptions[] = '<span title="' . htmlspecialchars($row['description'], ENT_QUOTES) .
             "' class='description'>\\0</span>";
    }
    $html = preg_replace($words, $descriptions, htmlspecialchars($text_from_db));
    

      Thanks! This works for the most part except the HTML span tag

      It's not completely correct as the HTML <span> that is generated on the first word is causing it not to appear.

      So...here's my text:
      Just an attempt to Beer Goggles and The Hammer to see how this works

      Here's what appears in the HTML page:
      Just an attempt to The Hammer to see how this works

      Notice Beer Goggles doesn't appear?

      Here's the HTML generated:

      Just an attempt to <span title="Goggles that cause a member of the.'>Beer Goggles</span> and <span title="Gettin&#039; your  banned from for good. A more severe GOOD NIGHT NOW!'>The Hammer</span> to see how this works   

      I can't quite figure out how to adjust the PHP string code with the ' " ', etc. to get the HTML correct. It's always the 1st word found that is missing...

      Mucho appreciate your guidance!!!

        It is due to a typo in NogDog's example. Notice that the string before the call to htmlspecialchars() has a double quote as its last character, yet the string after has a single quote as the first character. That single quote should be changed to a double quote, e.g., "\" class='description'>\0</span>".

          Write a Reply...