php explode all characters

PhpArraysExplode

Php Problem Overview


I'm looking for the equivalent of what in js would be 'this is a string'.split('') for PHP.

If I try $array = explode('', 'testing'); I get an error Warning: explode() [function.explode]: Empty delimiter in

Is there another way to do this?

Php Solutions


Solution 1 - Php

As indicated by your error, explode requires a delimiter to split the string. Use str_split instead:

$arr = str_split('testing');

Output

Array
(
    [0] => t
    [1] => e
    [2] => s
    [3] => t
    [4] => i
    [5] => n
    [6] => g
)

Solution 2 - Php

Use the str_split function.

$array = str_split( 'testing');

Solution 3 - Php

$string = "TEST";

echo $string[0];  // This will display T

There is no need to explode it

Solution 4 - Php

TO SPLIT string into ARRAY its best to use

$arr= str_split($string);

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
QuestionqwertymkView Question on Stackoverflow
Solution 1 - PhpJoshView Answer on Stackoverflow
Solution 2 - PhpnickbView Answer on Stackoverflow
Solution 3 - PhpMark RoachView Answer on Stackoverflow
Solution 4 - Phpuser3470929View Answer on Stackoverflow