Changeset 257


Ignore:
Timestamp:
May 16, 2007 5:36:18 AM (17 years ago)
Author:
quinn
Message:

Added a function to highlight words matching a search query highlightWords().

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/css/codebase.inc.css

    r107 r257  
    99 */
    1010
    11 /*_____________________ SC Messaging ____________________*/
     11/*_____________________ App.inc.php ____________________*/
    1212.sc-msg { font-family: verdana,geneva,arial,sans-serif; }
    1313
     
    2525.sc-msg-notice { color: #C60; border-color: #C60; }
    2626
    27 /*_____________________ PEdit Forms ____________________*/
     27/*_____________________ PEdit.inc.php ____________________*/
    2828
    2929.sc-pedit-form {}
     
    4949    white-space: nowrap;
    5050}
     51
     52/*_____________________ Utilities.inc.php ____________________*/
     53
     54.sc-highlightwords {
     55    background-color: #EBFE00;
     56    color: #000;
     57}
  • trunk/lib/Utilities.inc.php

    r255 r257  
    6767 * Returns text with appropriate html translations.
    6868 *
    69  * @param  string $txt              Text to clean.
     69 * @param  string $text             Text to clean.
    7070 * @param  bool   $preserve_html    If set to true, oTxt will not translage <, >, ", or '
    7171 *                                  characters into HTML entities. This allows HTML to pass
     
    7373 * @return string                   Cleaned text.
    7474 */
    75 function oTxt($txt, $preserve_html=false)
     75function oTxt($text, $preserve_html=false)
    7676{
    7777    $app =& App::getInstance();
     
    103103    $replace['ampersand']       = '&amp;';
    104104
    105     return preg_replace($search, $replace, htmlentities($txt, ENT_QUOTES, $app->getParam('character_set')));
     105    return preg_replace($search, $replace, htmlentities($text, ENT_QUOTES, $app->getParam('character_set')));
    106106}
    107107
     
    110110 * TODO: Allow a string such as this to be passted: <a href="javascript:openPopup('/foo/bar.php')">Click here</a>
    111111 *
    112  * @param  string   $txt Text to clean.
     112 * @param  string   $text Text to clean.
    113113 * @return string         Cleaned text.
    114114 */
    115 function fancyTxt($txt)
     115function fancyTxt($text)
    116116{
    117117    $search = array();
     
    138138    $replace['em_dash']         = '$1&mdash;$2';
    139139
    140     return preg_replace($search, $replace, $txt);
     140    return preg_replace($search, $replace, $text);
     141}
     142
     143/**
     144 * Applies a class to search terms to highlight them ala-google results.
     145 *
     146 * @param  string   $text   Input text to search.
     147 * @param  string   $search String of word(s) that will be highlighted.
     148 * @param  string   $class  CSS class to apply.
     149 * @return string           Text with searched words wrapped in <span>.
     150 */
     151function highlightWords($text, $search, $class='sc-highlightwords')
     152{
     153    $words = preg_split('/[^\w]/', $search, -1, PREG_SPLIT_NO_EMPTY);
     154   
     155    $search = array();
     156    $replace = array();
     157   
     158    foreach ($words as $w) {
     159        $search[] = '/\b(' . preg_quote($w) . ')\b/i';
     160        $replace[] = '<span class="' . $class . '">$1</span>';
     161    }
     162
     163    return preg_replace($search, $replace, $text);
    141164}
    142165
Note: See TracChangeset for help on using the changeset viewer.