Page Blocks

Hosting sponsored by:

Point In Space

 

API: Str

Filename:
fwpStr_highlight.ctag

Released With:
5.0.0

Current Version:
1.1.0

Status: Active

Min Lasso Tested: 8.1.0

Max Lasso Tested: 8.5.3

Related:

Still don't get it?

Check out the talk list archives, or join and ask your questions.

Documentation Error or Request?

Email documentation corrections or comments

API Reference

fwpStr_highlight (Tag)

Description

Highlights search terms in a supplied string. The text block should not contain any HTML or other markup to avoid the potential for search terms finding matches in the markup tags themselves.

Found search terms are surrounded by a span tag with the class name of "highlight."

Syntax

[fwpStr_highlight: 
   -terms=string: space delimited words, 
   -str=string: the text to search
   -ignorecase,
   -allowPartialWords]

Parameters & Member Tags

-terms - required : a space delimited list of words to be highlighted in the text. Only whole words will be hihlighted.

-str - required : the string to be searched and marked up with the span tags

-ignorecase - optional : changes the regex to ignore character case

-showSubWords - optional : normally, only whole words are highlighted, this option enables highlighting search strings as subwords

Examples

[var:'searchMe'='This is a test of the fwpStr_highlight tag to show that it
will highlight multiple search terms properly. It finds whole words only, and 
uses regex word boundaries in the search. The words \"search\" and \"test\"
should be highlighted. It can also be use with or without case sensitivity. 
The word "It" should be highlighted, but the word "it" should not.']

[var:'lookFor'='search test It']

<p>[fwpStr_highlight: -terms=$lookfor, -str=$searchMe]</p>

Source Code

View in separate window

<?lassoscript
//............................................................................
//
//    pageblocks: (c) 2002-2007 http://www.pageblocks.org/
//
//............................................................................
/*

    {fileName=        fwpStr_highlight.ctag }
    {rsrcType=        tag }
    {rsrcName=        fwpStr_highlight }
    {rsrcHTTP=        www.pageblocks.org/refc/fwpStr_highlight }

    {lassoVrsnMin=    8.1.0 }
    {lassoVrsnMax=    8.5.3 }

    {author=        Greg Willits }
    {authorEmail=    subscribe to pbTalk at www.pageblocks.org/talk/ }
    {authorHTTP=    www.pageblocks.org }

    {desc=            This tag returns the string 'str' with each substring 
                    listed in 'terms' marked up with a CSS span with a
                    "highlight" class name. }

    {maintvsrn=        1.1 }
    {maintrelease=    5.1.0 }
    {maintdate=        2006-08-30 }
    {maintauthor=    Greg Willits }
    {maintnotes=    added support for partial words }

    {maintvsrn=        1.0 }
    {maintrelease=    5.0.0 }
    {maintdate=        2006-01-16 }
    {maintauthor=    Greg Willits }
    {maintnotes=    initial release }

*/
//.............................................................................

define_tag:'fwpStr_highlight', -priority='replace',
    -required='terms',
    -required='str',
    -optional='ignorecase',
    -optional='showSubWords';

    local:
        'fw_srchTerms'    = @local:'terms',
        'fw_newTxt'        = local:'str',
        'fw_thisTerm'    = string,
        'fw_wordDelim'    = string;
        #fw_srchTerms    = #fw_srchTerms->split:' ';

    (params->contains:'-showSubWords')
        ? #fw_wordDelim = #fw_thisTerm
        | #fw_wordDelim = '\\b';

    if: (params)->find:'-ignorecase';

        iterate: #fw_srchTerms, #fw_thisTerm;
    
            #fw_newTxt=(string_replaceRegExp:
                #fw_newTxt,
                -find     = #fw_wordDelim + '(' + #fw_thisTerm + ')' + #fw_wordDelim,
                -replace = '<span class="highlight">\\1</span>',
                -ignorecase);
    
        /iterate;

    else;
    
        iterate: #fw_srchTerms, #fw_thisTerm;
    
            #fw_newTxt=(string_replaceRegExp:
                #fw_newTxt,
                -find     = #fw_wordDelim + '(' + #fw_thisTerm + ')' + #fw_wordDelim,
                -replace = '<span class="highlight">\\1</span>');
    
        /iterate;

    /if;

    return: @#fw_newTxt;

/define_tag;
?>

© 2002-2010, pageblocks.org