Retrieve a Portion of Long Text

December 31st, 2007

Often, I need to get the first few sentances of a long article or post to display as summary. Things to consider include not cutting off any partial words; add … if there is more; make sure html tags are stripped off otherwise we might bury an openning tag in the summary and that will screw up the entire page, etc.

Below is a function does just that. With configurable length to cut off.

function get_short_text ( $t_str, $n = 100 ) {
 // this function strips out all html tags, then take the first n chars of
 //
the string, but always stops at whole words
 
 // strip out html tags and javascripts
 $search_tags = array ("'<script[^>]*?>.*?</script>'si",  
                 "'<[/!]*?[^<>]*?>'si");         

 $replace_tags = array ("", "");  

 $t_str = preg_replace($search_tags, $replace_tags, $t_str);     

 if ( strlen( $t_str ) > $n ) {
     $t_str = substr( $t_str, 0, $n );
     // trim off partial word at the end
     $pos = strrpos( $t_str, ' ' );
     $t_str = substr ( $t_str, 0, $pos + 1 );
   }  

   return $t_str;
}

Please add your comment