note 101214 added to function.wordwrap
| From: | nbenitezl | Date: | Thu, 02 Dec 2010 12:56:08 +0000 |
| Subject: | note 101214 added to function.wordwrap | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-174560@lists.php.net to get a copy of this message | ||
Hi, this function is like wordwrap but it ignores html tags, it works like wordwrap when called with
fourth parameter as true. It's based on a function I find here but improved to closer match the
output of wordwrap (i.e. removed spaces at start of line) and also to improve performance.
Hope it can be useful for you :-)
<?php
function htmlwrap(&$str, $maxLength, $char='<br />'){
$count = 0;
$newStr = '';
$openTag = false;
$lenstr = strlen($str);
for($i=0; $i<$lenstr; $i++){
$newStr .= $str{$i};
if($str{$i} == '<'){
$openTag = true;
continue;
}
if(($openTag) && ($str{$i} == '>')){
$openTag = false;
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: 130.206.1.5
Probable Submitter: 82.150.0.6
----
Manual Page -- http://www.php.net/manual/en/function.wordwrap.php
Edit -- https://master.php.net/note/edit/101214
Del: integrated -- https://master.php.net/note/delete/101214/integrated
Del: useless -- https://master.php.net/note/delete/101214/useless
Del: bad code -- https://master.php.net/note/delete/101214/bad+code
Del: spam -- https://master.php.net/note/delete/101214/spam
Del: non-english -- https://master.php.net/note/delete/101214/non-english
Del: in docs -- https://master.php.net/note/delete/101214/in+docs
Del: other reasons-- https://master.php.net/note/delete/101214
Reject -- https://master.php.net/note/reject/101214
Search -- https://master.php.net/manage/user-notes.php