note 92063 added to function.substr
| From: | erez at meezoog dot com | Date: | Tue, 07 Jul 2009 16:59:59 +0000 |
| Subject: | note 92063 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-158022@lists.php.net to get a copy of this message | ||
An improvement to substrws - multi-byte, and closes tags better.
/**
* 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 mb_substrws( $text, $len=180 ) {
if( (mb_strlen($text) > $len) ) {
$whitespaceposition = mb_strpos($text," ",$len)-1;
if( $whitespaceposition > 0 ) {
$chars = count_chars(mb_substr($text, 0, ($whitespaceposition+1)), 1);
if ($chars[ord('<')] > $chars[ord('>')])
$whitespaceposition = mb_strpos($text,">",$whitespaceposition)-1;
$text = mb_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: 69.147.83.197
Probable Submitter: 62.0.88.252
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/92063
Del: integrated -- https://master.php.net/note/delete/92063/integrated
Del: useless -- https://master.php.net/note/delete/92063/useless
Del: bad code -- https://master.php.net/note/delete/92063/bad+code
Del: spam -- https://master.php.net/note/delete/92063/spam
Del: non-english -- https://master.php.net/note/delete/92063/non-english
Del: in docs -- https://master.php.net/note/delete/92063/in+docs
Del: other reasons-- https://master.php.net/note/delete/92063
Reject -- https://master.php.net/note/reject/92063
Search -- https://master.php.net/manage/user-notes.php