PHP:将字符串拆分为数组,就像没有分隔符的爆炸一样 [英] PHP: Split string into array, like explode with no delimiter
本文介绍了PHP:将字符串拆分为数组,就像没有分隔符的爆炸一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,例如:
I have a string such as:
0123456789"
"0123456789"
并且需要将每个字符拆分成一个数组.
and need to split EACH character into an array.
我真的试过了:
explode('', '123545789');
但它给了我显而易见的:警告:爆炸中没有定义分隔符) ..
But it gave me the obvious: Warning: No delimiter defined in explode) ..
我怎么会遇到这个?我看不到任何方法,尤其是一个函数
How would I come across this? I can't see any method off hand, especially just a function
推荐答案
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");
str_split 接受可选的第二个参数,块长度(默认1),因此您可以执行以下操作:
str_split takes an optional 2nd param, the chunk length (default 1), so you can do things like:
$array = str_split("aabbccdd", 2);
// $array[0] = aa
// $array[1] = bb
// $array[2] = cc etc ...
您还可以通过将字符串视为数组来获取部分字符串:
You can also get at parts of your string by treating it as an array:
$string = "hello";
echo $string[1];
// outputs "e"
这篇关于PHP:将字符串拆分为数组,就像没有分隔符的爆炸一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文