Convert a String into an Array of Characters

PhpArraysString

Php Problem Overview


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

Php Solutions


Solution 1 - Php

You will want to use str_split().

$result = str_split('abcdef');

http://us2.php.net/manual/en/function.str-split.php

Solution 2 - Php

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.

Solution 3 - Php

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.

Solution 4 - Php

$result = "abcdef";
$result = str_split($result);

There is also an optional parameter on the str_split function to split into chunks of x characters.

Solution 5 - Php

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
)

Solution 6 - Php

With the help of str_split function, you will do it.

Like below::

<?php 
$result = str_split('abcdef',1);
echo "<pre>";
print_r($result);
?>

Solution 7 - Php

You can use the str_split() function

$array = str_split($string);

foreach ($array as $p){

    echo $p . "<br />";
}

Solution 8 - Php

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.

Solution 9 - Php

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) which respects newline characters by default. \X is slightly different from . without the s modifier. Demo

var_export(preg_split('/.\K/u', $string, 0, PREG_SPLIT_NO_EMPTY)); // element [1] has two characters in it!
echo "\n---\n";
var_export(preg_split('/.\K/us', $string, 0, PREG_SPLIT_NO_EMPTY));
echo "\n---\n";
var_export(preg_split('/\X\K/u', $string, 0, PREG_SPLIT_NO_EMPTY));

Solution 10 - Php

str_split() is not safe for multibyte characters.

mb_str_split() requires PHP 7.4+.

Try preg_split() for the rescuse:

$result = preg_split('/(.)/u', 'abcåäö', -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAJ.View Question on Stackoverflow
Solution 1 - PhpduskView Answer on Stackoverflow
Solution 2 - PhpBT643View Answer on Stackoverflow
Solution 3 - PhpChaimView Answer on Stackoverflow
Solution 4 - PhpGazlerView Answer on Stackoverflow
Solution 5 - PhpGautam RaiView Answer on Stackoverflow
Solution 6 - PhpPratikView Answer on Stackoverflow
Solution 7 - PhpNazcaView Answer on Stackoverflow
Solution 8 - PhpGTCraisView Answer on Stackoverflow
Solution 9 - PhpmickmackusaView Answer on Stackoverflow
Solution 10 - PhptimView Answer on Stackoverflow