note 60132 added to function.explode
| From: | sbc at mail dot bg | Date: | Tue, 27 Dec 2005 17:11:22 +0000 |
| Subject: | note 60132 added to function.explode | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-100802@lists.php.net to get a copy of this message | ||
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!
----
Manual Page -- http://www.php.net/manual/en/function.explode.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60132
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60132&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60132&report=yes
Search -- http://master.php.net/manage/user-notes.php