note 16771 deleted from function.chunk-split by jimw
| From: | jimw@php.net | Date: | Fri, 07 Dec 2001 01:58:41 +0000 |
| Subject: | note 16771 deleted from function.chunk-split by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-21793@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);
}