note 34513 added to function.array-slice
| From: | eric at rack1 dot php dot net | Date: | Tue, 29 Jul 2003 17:05:52 +0000 |
| Subject: | note 34513 added to function.array-slice | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-52949@lists.php.net to get a copy of this message | ||
I'm new at this, and I didn't get any help from my friend Ryan, but this works great for
breaking a string up into fractions. I used it for creating faux columns from one string on an html
page. Enjoy!
function fraction_words($numerator,$denominator,$string){
$string_as_array = explode(" ",$string);
$wordcount = str_word_count($string);
$offset = ceil((($wordcount/$denominator)*$numerator) - ($wordcount/$denominator));
$length = ceil($wordcount/$denominator);
$fraction_words = join(" ",array_slice($string_as_array,$offset,$length));
return $fraction_words;
}
echo fraction_words(1,4,$your_string);
This will return the first fourth of $your_string.
Anybody smart out there...please help me with my boolean logic for the offset calculation if
it's redundant...
----
Manual Page -- http://www.php.net/manual/en/function.array-slice.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+34513
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+34513&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+34513&report=yes