php explode all characters [duplicate]

放肆的年华 提交于 2019-11-27 15:28:06

问题


This question already has an answer here:

  • PHP: Split string into array, like explode with no delimiter 9 answers

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?


回答1:


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
)



回答2:


Use the str_split function.

$array = str_split( 'testing');



回答3:


$string = "TEST";

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

There is no need to explode it




回答4:


TO SPLIT string into ARRAY its best to use

$arr= str_split($string);


来源:https://stackoverflow.com/questions/9814375/php-explode-all-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!