note 103582 added to function.wordwrap
| From: | benallfree at gmail dot com | Date: | Thu, 21 Apr 2011 03:08:34 +0000 |
| Subject: | note 103582 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-179037@lists.php.net to get a copy of this message | ||
Bug fix to previous version which did not account for <br> tags in HTML introducing a visible
space.
function htmlwrap(&$str, $maxLength=70, $char="<br />\n"){
$count = 0;
$newStr = '';
$openTag = false;
$lenstr = strlen($str);
$last_tag = "";
for($i=0; $i<$lenstr; $i++){
$newStr .= $str{$i};
if($openTag) $last_tag.=$str{$i};
if($str{$i} == '<'){
$last_tag = "<";
$openTag = true;
continue;
}
if(($openTag) && ($str{$i} == '>')){
$openTag = false;
if(preg_match("/^<\s*br\s*/", $last_tag)!=0)
{
$lastspace=0;
$count=0;
}
continue;
}
if(!$openTag){
if($str{$i} == ' '){
if ($count == 0) {
$newStr = substr($newStr,0, -1);
continue;
} else {
$lastspace = $count + 1;
}
}
$count++;
if($count==$maxLength){
if ($str{$i+1} != ' ' && $lastspace && ($lastspace <
$count)) {
$tmp = ($count - $lastspace)* -1;
$newStr = substr($newStr,0, $tmp) . $char . substr($newStr,$tmp);
$count = $tmp * -1;
} else {
$newStr .= $char;
$count = 0;
$lastspace = 0;
}
}
}
}
return $newStr;
}
----
Server IP: 69.147.83.197
Probable Submitter: 72.194.208.80
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/103582
Del: integrated -- https://master.php.net/note/delete/103582/integrated
Del: useless -- https://master.php.net/note/delete/103582/useless
Del: bad code -- https://master.php.net/note/delete/103582/bad+code
Del: spam -- https://master.php.net/note/delete/103582/spam
Del: non-english -- https://master.php.net/note/delete/103582/non-english
Del: in docs -- https://master.php.net/note/delete/103582/in+docs
Del: other reasons-- https://master.php.net/note/delete/103582
Reject -- https://master.php.net/note/reject/103582
Search -- https://master.php.net/manage/user-notes.php