note 48401 deleted from function.wordwrap by danbrown

From: Date: Sun, 17 May 2009 18:36:28 +0000
Subject: note 48401 deleted from function.wordwrap by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155146@lists.php.net to get a copy of this message
Note Submitter: me at home dot com ---- please improve this function i have made to wrap text wider than $need_width pixels... function wrap_pix($text,$need_width,$separator,$font,$font_size, $dim){ $len = strlen($text); $width = 65000; $len_left = $interval = floor($len/2); do { $left = substr($text,0,$len_left); if ($left[0] == " ") { $left = substr($left,1); $text = substr($text,1); } if ( $left[strlen($left)] == " "){ $left = substr($left,0,strlen($left)-1); } $size = imagettfbbox($font_size,0,$font,$left); $width = abs($size[2] - $size[0]); $interval = 1 > floor($interval/2) ? 1 : floor($interval/2); if ( $width <= $need_width-$dim && strlen($left)!=strlen($text)){ $to = $len < strlen($left)+$interval ? $len : strlen($left)+$interval; $left = substr($text,0,$to); $len_left = strlen($left); continue; } if ( $width > $need_width+$dim ){ $left = substr($text,0,strlen($left)-$interval); $len_left = strlen($left); continue; } } while ( (($width <= $need_width-$dim) || ($width > $need_width+$dim)) && strlen($text) != strlen($left)); if ( $width > $need_width ){ if (strrchr($left," ")){ $newlen = strlen($left) - strlen(strrchr($left," ")); $left = substr($left,0,$newlen).$separator; $minus=0; } else{ $left = substr($left,0,$len_left-2)."-".$separator; $minus = 1; } } if ( $width <= $need_width){ if ( preg_match("/\.,\-\s\"\'/i",$text[strlen($left)+1])){ $tmp = substr($text,0,strlen($left)+1); $size = imagettfbbox($fontsize,0,$font,$tmp); $width = abs($size[2] - $size[0]); if ( $width < $need_width ) $left = $tmp; } else if ( !preg_match("/\.,\-\s\"\'/i",$text[strlen($left)])){ $newlen = strlen($left) - strlen(strrchr($left," ")); $left = substr($left,0,$newlen).$separator; } } if (strlen($text)!=0) { $return=wrap_pix( substr($text,(strlen($left)-strlen($separator)-$minus)), $need_width,$separator,$font,$font_size,$dim); $left.=$return; } return $left; }

« previous php.notes (#155146) next »