Development

Changeset 8392

You must first sign up to be able to contribute.

Changeset 8392

Show
Ignore:
Timestamp:
04/10/08 17:32:05 (6 months ago)
Author:
FabianLange
Message:

added excert_space option to excerpt_text in TextHelper?.php, backwards compatible set to default "false" allowing to correct problems where excerpt_text might destroy html entities (or other stuff that should not be truncated) Added 3 Unit tests for that. (closes #3303)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.0/lib/helper/TextHelper.php

    r3699 r8392  
    7272 * by +radius+. If the phrase isn't found, nil is returned. Ex: 
    7373 *   excerpt("hello my world", "my", 3) => "...lo my wo..." 
    74  */ 
    75 function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...') 
     74 * If +excerpt_space+ is true the text will only be truncated on whitespace, never inbetween words. 
     75 * This might return a smaller radius than specified. 
     76 *   excerpt("hello my world", "my", 3, "...", true) => "... my ..." 
     77 */ 
     78function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...', $excerpt_space = false) 
    7679{ 
    7780  if ($text == '' || $phrase == '') 
     
    8588    $start_pos = max($found_pos - $radius, 0); 
    8689    $end_pos = min($found_pos + strlen($phrase) + $radius, strlen($text)); 
     90    $excerpt = substr($text, $start_pos, $end_pos - $start_pos); 
    8791 
    8892    $prefix = ($start_pos > 0) ? $excerpt_string : ''; 
    8993    $postfix = $end_pos < strlen($text) ? $excerpt_string : ''; 
    9094 
    91     return $prefix.substr($text, $start_pos, $end_pos - $start_pos).$postfix; 
     95    if ($excerpt_space) 
     96    { 
     97      //only cut off at ends where $exceprt_string is added 
     98      if($prefix) 
     99      { 
     100        $excerpt = preg_replace('/^(\S+)?\s+?/', ' ', $excerpt); 
     101      } 
     102      if($postfix) 
     103      { 
     104        $excerpt = preg_replace('/\s+?(\S+)?$/', ' ', $excerpt); 
     105      } 
     106 
     107    } 
     108 
     109    return $prefix.$excerpt.$postfix; 
    92110  } 
    93111} 
  • branches/1.0/test/unit/helper/TextHelperTest.php

    r3699 r8392  
    1313sfLoader::loadHelpers(array('Helper', 'Tag', 'Text')); 
    1414 
    15 $t = new lime_test(43, new lime_output_color()); 
     15$t = new lime_test(45, new lime_output_color()); 
    1616 
    1717// truncate_text() 
     
    7474$t->is(excerpt_text("This is a beautiful morning", "this", 5), "This is a...", 'text_excerpt() creates an excerpt of a text'); 
    7575$t->is(excerpt_text("This is a beautiful morning", "morning", 5), "...iful morning", 'text_excerpt() creates an excerpt of a text'); 
    76 $t->is(excerpt_text("This is a beautiful morning", "morning", 5), "...iful morning", 'text_excerpt() creates an excerpt of a text'); 
     76$t->is(excerpt_text("This is a beautiful morning", "morning", 5, '...', true), "... morning", 'text_excerpt() takes a fifth argument allowing excerpt on whitespace'); 
     77$t->is(excerpt_text("This is a beautiful morning", "beautiful", 5, '...', true), "... a beautiful ...", 'text_excerpt() takes a fifth argument allowing excerpt on whitespace'); 
     78$t->is(excerpt_text("This is a beautiful morning", "This", 5, '...', true), "This is ...", 'text_excerpt() takes a fifth argument allowing excerpt on whitespace'); 
    7779$t->is(excerpt_text("This is a beautiful morning", "day"), '', 'text_excerpt() does nothing if the search string is not in input'); 
    7880