note 35828 deleted from function.wordwrap by aidan
| From: | aidan@php.net | Date: | Mon, 19 Jul 2004 23:25:48 +0000 |
| Subject: | note 35828 deleted from function.wordwrap by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73647@lists.php.net to get a copy of this message | ||
Note Submitter: php@nev-hilft.de
----
Hi @ll
I have modify one of the previews posted functions.
This version have the wordwrap option [cut], so it split no words, which are not longer the
max-char-lenght.
This function doesn't cut in HTML tags, just in the plain text.
function short_words($str,$cols,$cut) {
$str = str_replace("\r\n","",$str);
$str = str_replace("\n","",$str);
$str = str_replace("\r","",$str);
$tag_open = '<';
$tag_close = '>';
$in_tag = 0;
$str_len = strlen($str);
$segment_width = 0;
for ($i=0; $i <= $str_len; $i++){
if (substr($str,$i,strlen($cut)) == $cut) {
$i += strlen($cut)-1;
$segment_width = 0;
} else {
if ($str[$i] == $tag_open) {
$in_tag++;
} elseif ($str[$i] == $tag_close) {
if ($in_tag > 0) {
$in_tag--;
}
} else {
if ($in_tag == 0) {
$segment_width++;
if ($str[$i] == " ") {
$last_space = $i;
}
if (($segment_width > $cols) && ($str[$i] != " ")) {
if ($last_space < $i) {
$str = substr($str,0,$last_space).$cut.substr($str,$last_space);
$i = $last_space + strlen($cut) - 1;
} else {
$str = substr($str,0,$i).$cut.substr($str,$i);
$i += strlen($cut) - 1;
}
$last_space = 0;
$str_len = strlen($str);
$segment_width = 0;
}
}
}
}
}
return $str;
}
$str = short_words($str, 45 ,"<br />");
Cu
Nev