To split UTF-8 into 16-charcater slices you can use:<?phppreg_split('/(?=(.{16})*$)/u', $text);?>You can also use look-behind instead of look-ahead if you want the first (not the last) chunk to be the shortest.

The documentation fails to mention what happens when the string length does not divide evenly with the chunk size. Not sure if the same behavior for all versions of PHP so I offer the following code to determine this for your installation. On mine [version 5.2.17], the last chunk is an array the length of the remaining chars.

The very handy str_split() was introduced in PHP 5, but a lot of us are still forced to use PHP 4 at our host servers. And I am sure a lot of beginners have looked or are looking for a function to accomplish what str_split() does.

Taking advantge of the fact that strings are 'arrays' I wrote this tiny but useful e-mail cloaker in PHP, which guarantees functionality even if JavaScript is disabled in the client's browser. Watch how I make up for the lack of str_split() in PHP 4.3.10.

// We create a new array called $arChars, which will contain the individula characters making up the email address. The array is blank for now.$arChars = array();

// We extract each character from the email 'exploiting' the fact that strings behave like an array: watch the '$email[$i]' bit, and beging to fill up the blank array $arCharsfor ($i = 0; $i < strlen($email); $i++) { $arChars[] = $email[$i]; }

// Now we work on the $arChars array: extract each character in the array and print out it's ASCII value prefixed with '&#' to convert it into an HTML entityforeach ($arChars as $char) { print '&#'.ord($char); }

// The result is an email address in HTML entities which, I hope most email address harvesters can't read.

In creating this little utility, I demonstrated how the lack of str_split() can be made up in PHP < 5. If you got how it was accomplished, you could write a function to do exactly what str_split() does in PHP 5 and even name it 'str_split()'. :)

It's mentioned in the Return Values section above ("If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element"), but note that an input of empty string will return array(1) { [0]=> string(0) "" }. Interestingly an input of NULL will also return array(1) { [0]=> string(0) "" }.

Compare this with, say, <?php preg_split('//', $inputString, -1, PREG_SPLIT_NO_EMPTY); ?> which will return array(0) { } for an input of empty string or NULL. I find this to be a bit more intuitive.

I'm not sure if this can be considered a bug, since this is due to how type conversion and casting works, so i just posted it here.I've notced that this is how strval() works. Can anyone shed light into this?..

Regarding ricordatis comment on preg_match_all('/./u',...) instead of preg_split('//u',...):You'll have to use the pattern '/./us' with preg_match_all to get exactly the same behaviour w.r.t. newlines. Don't know if this is still faster, though. Oh, and the expected result is in $array[0].