note 16771 added to function.chunk-split
| From: | vik at matfyz dot cz | Date: | Tue, 13 Nov 2001 00:11:52 +0000 |
| Subject: | note 16771 added to function.chunk-split | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-20771@lists.php.net to get a copy of this message | ||
<P>I need to divide a long string into an array of smaller chunks. If I use the function
chunk_split(), I will need to use the function ereg_replace() or explode() to get the array. There
is a problem though. What to use as a delimiter in chunk_split() that cannot already be in the long
string? (e.g. If I use '\n' as the delimiter in chunk_split(), the string could have
already contained some '\n' and therefore when I use explode(), I will get more chunks
than I necessarily need.)
</P>
Well, this is the way I go around it.<br>
$chunk_length = 250;
$nr_chunks = ceil(strle ($long_string) / $chunk_length);
for ($ix=0; $ix<$nr_chunks; $ix++) {
$chunk[$ix] = substr ($long_string, $ix*$chunk_length, $chunk_length);
}
--
http://www.php.net/manual/en/function.chunk-split.php
http://master.php.net/manage/user-notes.php?action=edit+16771
http://master.php.net/manage/user-notes.php?action=delete+16771
http://master.php.net/manage/user-notes.php?action=reject+16771