note 36303 deleted from function.wordwrap by aidan
| From: | aidan@php.net | Date: | Mon, 19 Jul 2004 23:25:39 +0000 |
| Subject: | note 36303 deleted from function.wordwrap by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73646@lists.php.net to get a copy of this message | ||
Note Submitter:
----
<?php
function new_textwrap ($String, $breaksAt = 78, $breakStr = "\n", $cut = 1 ,
$padStr="") {
/*
new_textwrap 1.0 <linker@toter-link.de>
Based on textwrap 1.0 by Brian Moon <brian@phorum.org>
http://px.sklar.com/code-pretty.html?code_id=370
tested with PHP 3.0.16 WIN
You can use it as ' wordwrap ' from PHP4
$String The string to be wrapped.
$breaksAt How many characters each line should be.
$breakStr What character should be used to cause a break.
$cut If the cut is set to 1, the string is always wrapped at the specified width.
$padStr Allows for the wrapped lines to be padded at the begining.
*/
$newString="";
$lines=explode($breakStr, $String);
$cnt=count($lines);
for($x=0;$x<$cnt;$x++){
if(strlen($lines[$x])>$breaksAt){
$str=$lines[$x];
while(strlen($str)>$breaksAt){
$find = 1 ;
// echo $str."\n" ;
$pos=strrpos(substr($str, 0, $breaksAt+1), " ");
if ($pos == false) {
If($cut) {
$pos = $breaksAt ;
$find = 0 ;
} else {
$pos= strpos($str, " ");
if ($pos == false)
break;
}
}
$newString.=$padStr.substr($str, 0, $pos).$breakStr;
$str=(substr($str, $pos + $find)); // or $str=trim(substr($str, $pos));
}
$newString.=$padStr.$str.$breakStr;
}
else{
$newString.=$padStr.$lines[$x].$breakStr;
}
}
return substr ($newString,0, -(strlen($breakStr)));
} // end new_textwrap()
?>