Changeset 8392
- Timestamp:
- 04/10/08 17:32:05 (6 months ago)
- Files:
-
- branches/1.0/lib/helper/TextHelper.php (modified) (2 diffs)
- branches/1.0/test/unit/helper/TextHelperTest.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/1.0/lib/helper/TextHelper.php
r3699 r8392 72 72 * by +radius+. If the phrase isn't found, nil is returned. Ex: 73 73 * 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 */ 78 function excerpt_text($text, $phrase, $radius = 100, $excerpt_string = '...', $excerpt_space = false) 76 79 { 77 80 if ($text == '' || $phrase == '') … … 85 88 $start_pos = max($found_pos - $radius, 0); 86 89 $end_pos = min($found_pos + strlen($phrase) + $radius, strlen($text)); 90 $excerpt = substr($text, $start_pos, $end_pos - $start_pos); 87 91 88 92 $prefix = ($start_pos > 0) ? $excerpt_string : ''; 89 93 $postfix = $end_pos < strlen($text) ? $excerpt_string : ''; 90 94 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; 92 110 } 93 111 } branches/1.0/test/unit/helper/TextHelperTest.php
r3699 r8392 13 13 sfLoader::loadHelpers(array('Helper', 'Tag', 'Text')); 14 14 15 $t = new lime_test(4 3, new lime_output_color());15 $t = new lime_test(45, new lime_output_color()); 16 16 17 17 // truncate_text() … … 74 74 $t->is(excerpt_text("This is a beautiful morning", "this", 5), "This is a...", 'text_excerpt() creates an excerpt of a text'); 75 75 $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'); 77 79 $t->is(excerpt_text("This is a beautiful morning", "day"), '', 'text_excerpt() does nothing if the search string is not in input'); 78 80