PHP:将字符串拆分为数组,就像没有分隔符的爆炸一样
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2170320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP: Split string into array, like explode with no delimiter
提问by oni-kun
I have a string such as:
我有一个字符串,例如:
"0123456789"
“0123456789”
and need to split EACH character into an array.
并且需要将每个字符拆分成一个数组。
I for the hell of it tried:
我真的尝试过:
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
我怎么会遇到这个?我看不到任何方法,尤其是一个函数
回答by Erik
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");
str_splittakes an optional 2nd param, the chunk length (default 1), so you can do things like:
str_split采用可选的第二个参数,块长度(默认为 1),因此您可以执行以下操作:
$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"
回答by nategood
What are you trying to accomplish? You can access characters in a string just like an array:
你想达到什么目的?您可以像访问数组一样访问字符串中的字符:
$s = 'abcd';
echo $s[0];
prints 'a'
打印“一个”
回答by Conan
Try this:
尝试这个:
$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
回答by Soufiane Hassou
str_splitcan do the trick. Note that strings in PHP can be accessed just like a chars array, in most cases, you won't need to split your string into a "new" array.
str_split可以做到这一点。请注意,PHP 中的字符串可以像字符数组一样访问,在大多数情况下,您不需要将字符串拆分为“新”数组。
回答by Danijel
Here is an example that works with multibyte ( UTF-8 ) strings.
这是一个适用于多字节 (UTF-8) 字符串的示例。
$str = '?bcd';
// PHP 5.4.8 allows null as the third argument of mb_strpos() function
do {
$arr[] = mb_substr( $str, 0, 1, 'utf-8' );
} while ( $str = mb_substr( $str, 1, mb_strlen( $str ), 'utf-8' ) );
It can be also done with preg_split()( preg_split( '//u', $str, null, PREG_SPLIT_NO_EMPTY )), but unlike the above example, that runs almost as fast regardless of the size of the string, preg_split()is fast with small strings, but a lot slower with large ones.
也可以使用preg_split()( preg_split( '//u', $str, null, PREG_SPLIT_NO_EMPTY ))来完成,但与上面的示例不同,无论字符串的大小如何,它的运行速度几乎一样快,preg_split()对于小字符串来说很快,但对于大字符串来说要慢得多。
回答by Reza Roshan
Try this:
尝试这个:
$str = "Hello Friend";
$arr1 = str_split($str);
$arr2 = str_split($str, 3);
print_r($arr1);
print_r($arr2);
The above example will output:
上面的例子将输出:
Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => i
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => Hel
[1] => lo
[2] => Fri
[3] => end
)
回答by Rekha
Try this:
尝试这个:
$str = '546788';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
回答by user3470929
If you want to split the string, it's best to use:
如果要拆分字符串,最好使用:
$array = str_split($string);
When you have delimiter, which separates the string, you can try,
当您有分隔字符串的分隔符时,您可以尝试,
explode('' ,$string);
Where you can pass the delimiter in the first variable inside the explode such as:
您可以在其中传递爆炸内部第一个变量中的分隔符,例如:
explode(',',$string);
回答by Nedzad Ganic
$array = str_split("$string");
will actuall work pretty fine, BUT if you want to preserve the special characters in that string, and you want to do some manipulation with them, THAN I would use
实际上会工作得很好,但是如果您想保留该字符串中的特殊字符,并且您想对它们进行一些操作,那么我会使用
do {
$array[] = mb_substr( $string, 0, 1, 'utf-8' );
} while ( $string = mb_substr( $string, 1, mb_strlen( $string ), 'utf-8' ) );
because for some of mine personal uses, it has been shown to be more reliable when there is an issue with special characters
因为对于我的一些个人用途,它已被证明在出现特殊字符问题时更可靠

