Voting

: one plus five?
(Example: nine)

The Note You're Voting On

gert dot matern at web dot de
15 years ago
We are talking about Multi Byte ( e.g. UTF-8) strings here, so preg_split will fail for the following string:

'Weiße Rosen sind nicht grün!'

And because I didn't find a regex to simulate a str_split I optimized the first solution from adjwilli a bit:

<?php
$string
= 'Weiße Rosen sind nicht grün!'
$stop = mb_strlen( $string);
$result = array();

for(
$idx = 0; $idx < $stop; $idx++)
{
$result[] = mb_substr( $string, $idx, 1);
}
?>

Here is an example with adjwilli's function:

<?php
mb_internal_encoding
( 'UTF-8');
mb_regex_encoding( 'UTF-8');

function
mbStringToArray
( $string
)
{
$stop = mb_strlen( $string);
$result = array();

for(
$idx = 0; $idx < $stop; $idx++)
{
$result[] = mb_substr( $string, $idx, 1);
}

return
$result;
}

echo
'<pre>', PHP_EOL,
print_r( mbStringToArray( 'Weiße Rosen sind nicht grün!', true)), PHP_EOL,
'</pre>';
?>

Let me know [by personal email], if someone found a regex to simulate a str_split with mb_split.

<< Back to user notes page

To Top