In PHP, how do I convert:
$result = "abdcef";
into an array that's:
$result[0] = a;
$result[1] = b;
$result[2] = c;
$result[3] = d;
Edited
You will want to use str_split().
$result = str_split('abcdef');
Don't know if you're aware of this already, but you may not need to do anything (depending on what you're trying to do).
$string = "abcdef";
echo $string[1];
//Outputs "b"
So you can access it like an array without any faffing if you just need something simple.
You can use the str_split() function:
$value = "abcdef";
$array = str_split($value);
If you wish to divide the string into array values of different amounts you can specify the second parameter:
$array = str_split($value, 2);
The above will split your string into an array in chunks of two.
best you should go for "str_split()", if there is need to manual Or basic programming,
$string = "abcdef";
$resultArr = [];
$strLength = strlen($string);
for ($i = 0; $i < $strLength; $i++) {
$resultArr[$i] = $string[$i];
}
print_r($resultArr);
Output:
Array
(
[0] => a
[1] => b
[2] => c
[3] => d
[4] => e
[5] => f
)
https://www.php.net/manual/en/function.str-split.php#refsect1-function.str-split-notes
str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string.
Use mb_str_split() instead.
If you need multibyte support in an outdated version of PHP (a version below PHP7.4), then use preg_split() on an empty pattern with a unicode flag. There is no need to slow down the regex engine with a capture group.
Code: (Demo)
var_export(
preg_split('//u', 'abcåäö', 0, PREG_SPLIT_NO_EMPTY)
// or preg_split('/.\K/us', 'abcåäö', 0, PREG_SPLIT_NO_EMPTY)
// or preg_split('/\X\K/u', 'abcåäö', 0, PREG_SPLIT_NO_EMPTY)
);
For any versions of PHP from 7.4 or higher, just use the dedicated function mb_str_split().
mb_str_split('abcåäö')
Output:
array (
0 => 'a',
1 => 'b',
2 => 'c',
3 => 'å',
4 => 'ä',
5 => 'ö',
)
As a warning to researchers using other answers on this page, if you use square brace syntax to access characters by their offset or you use str_split(), you will get broken results when dealing with multibyte characters.
For anyone doing thorough research, I should also mention the \X (unicode version of the dot) respects newline characters by default. \X is slightly different from . without the s modifier. Demo
var_export(preg_split('/.\K/u', "å\nä", 0, PREG_SPLIT_NO_EMPTY)); // element [1] has two characters in it!
echo "\n---\n";
var_export(preg_split('/.\K/us', "å\nä", 0, PREG_SPLIT_NO_EMPTY));
echo "\n---\n";
var_export(preg_split('/\X\K/u', "å\nä", 0, PREG_SPLIT_NO_EMPTY));
Update as of 10 Apr 2024:
grapheme_str_split() has been unanimously approved and is proposed for PHP8.4. If the goal is to split the string AND keep "clustered" entities in tact, then this is the most ideal tool.
$string = 'cå🙇♂️';
var_export(grapheme_str_split($string));
// ['c', 'å', '🙇♂️']
// it does not create: ['c', 'å', '🙇', '', '♂', '']
If you tried the above ways and did not get results, try the following method. It worked for my language (Persian and Arabic):
$result = [];
for ( $i = 0; $i < mb_strlen( $string ); ++ $i ) {
$result[] = mb_substr( $string, $i, 1, 'UTF-8' );
}
var_dump($result);