note 65981 deleted from function.wordwrap by danbrown

From: Date: Sun, 17 May 2009 18:31:26 +0000
Subject: note 65981 deleted from function.wordwrap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155130@lists.php.net to get a copy of this message
Note Submitter: Paolo Stefan ---- Hi everyone, I have modified textWrap(); this one works just fine (the last one had a buggy regular expression!) and also gives you the chance to choose the line separator (3rd param). Enjoy :) <?php function textWrap($text, $size, $separator="\\r\\n" ) { $new_text = ''; $text_1 = explode('>',$text); $sizeof = sizeof($text_1); $remain = $size; for ($i=0; $i<$sizeof; ++$i) { $text_2 = explode('<',$text_1[$i]); if (!empty($text_2[0])){ $perl = '/([^\\n\\r .]{'. $remain .',})/i'; $possibly_splitted= preg_replace( $perl, '$1'.$separator, $text_2[0] ); $splitted = explode($separator,$possibly_splitted); $remain -= strlen($splitted[0]); if($remain<=0) $remain = $size; $new_text .= $possibly_splitted; } if (!empty($text_2[1])) $new_text .= '<' . $text_2[1] . '>'; } return $new_text; } ?>

« previous php.notes (#155130) next »