note 60132 deleted from function.explode by bjori

From: Date: Mon, 25 Sep 2006 08:06:41 +0000
Subject: note 60132 deleted from function.explode by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-117570@lists.php.net to get a copy of this message
Note Submitter: sbc at mail dot bg ---- I`m sorry, the previous function was very slow because it uses expandable number of iterations, and does not hyphenate correctly. so use this one, because it uses only 1 iteration!!! Also keep in mind that the function hyphenate whole words. <? function hyphenate_text($text) { $length = 70; // Length of the rows $hyphenate = '<br>'; // Hyphenate char $startcount = 0; $curindex = 0; $wcount = 0; $words = explode(' ', $text); $c = count ($words); $hyphe = array(); for($i=0; $i<=$c; $i++) { $value = $words[$i]; $wcount += strlen($value)+1; if($wcount>$length-strlen($hyphenate) || $i==$c) { if ($curindex==0) // If there is word bigger than the length { $curindex=1; $i++; } $med_line = implode(' ', array_slice($words, $startcount, $curindex)) . $hyphenate; array_push ($hyphe, $med_line); // Add Line $wcount = 0; $startcount += $curindex; $curindex = -1; if ($i == $c) break; $i--; } $curindex++; } return $hyphe; } ?> You have to add String, and the function returns array of lines!

« previous php.notes (#117570) next »