note 21170 added to function.wordwrap
| From: | jeff at wecreate dot ca | Date: | Wed, 01 May 2002 19:57:48 +0000 |
| Subject: | note 21170 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-30061@lists.php.net to get a copy of this message | ||
If you want to skip HTML tags you could use something like this...
function better_wordwrap($str,$cols,$cut){
$tag_open = '<';
$tag_close = '>';
$count = 0;
$in_tag = 0;
$str_len = strlen($str);
$segment_width = 0;
for ($i=1 ; $i<=$str_len ; $i++){
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 (($segment_width > $cols) && ($str[$i] == " ")) {
$str = substr($str,0,$i).$cut.substr($str,$i+1,$str_len-1);
$i += strlen($cut);
$str_len = strlen($str);
$segment_width = 0;
}
}
}
}
return $str;
}
Jeffrey Sambells
http://www.wecreate.ca
--
http://www.php.net/manual/en/function.wordwrap.php
http://master.php.net/manage/user-notes.php?action=edit+21170
http://master.php.net/manage/user-notes.php?action=delete+21170
http://master.php.net/manage/user-notes.php?action=reject+21170