note 90724 added to function.substr

From: Date: Wed, 06 May 2009 08:52:16 +0000
Subject: note 90724 added to function.substr
Groups: php.notes 
Request: Send a blank email to php-notes+get-154275@lists.php.net to get a copy of this message
If you need a word-sensitive and also html-tags aware version of substr, this one should do the job. It works fine for me /** * word-sensitive substring function with html tags awareness * @param text The text to cut * @param len The maximum length of the cut string * @returns string **/ function substrws( $text, $len=180 ) { if( (strlen($text) > $len) ) { $whitespaceposition = strpos($text," ",$len)-1; if( $whitespaceposition > 0 ) $text = substr($text, 0, ($whitespaceposition+1)); // close unclosed html tags if( preg_match_all("|<([a-zA-Z]+)>|",$text,$aBuffer) ) { if( !empty($aBuffer[1]) ) { preg_match_all("|</([a-zA-Z]+)>|",$text,$aBuffer2); if( count($aBuffer[1]) != count($aBuffer2[1]) ) { foreach( $aBuffer[1] as $index => $tag ) { if( empty($aBuffer2[1][$index]) || $aBuffer2[1][$index] != $tag) $text .= '</'.$tag.'>'; } } } } } return $text; } ---- Server IP: 217.160.72.57 Probable Submitter: 194.97.106.98 ---- Manual Page -- http://www.php.net/manual/en/function.substr.php Edit -- https://master.php.net/note/edit/90724 Del: integrated -- https://master.php.net/note/delete/90724/integrated Del: useless -- https://master.php.net/note/delete/90724/useless Del: bad code -- https://master.php.net/note/delete/90724/bad+code Del: spam -- https://master.php.net/note/delete/90724/spam Del: non-english -- https://master.php.net/note/delete/90724/non-english Del: in docs -- https://master.php.net/note/delete/90724/in+docs Del: other reasons-- https://master.php.net/note/delete/90724 Reject -- https://master.php.net/note/reject/90724 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#154275) next »