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) ..
但它给了我一个明显的提示:警告:在blow中没有定义分隔符。
How would I come across this? I can't see any method off hand, especially just a function
我怎么会遇到这种情况呢?我不能马上看到任何方法,尤其是一个函数
9 个解决方案
#1
124
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");
str_split takes 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"
#2
19
What are you trying to accomplish? You can access characters in a string just like an array:
你想要完成什么?您可以访问字符串中的字符,就像数组一样:
$s = 'abcd';
echo $s[0];
prints 'a'
打印“a”
#3
6
Try this:
试试这个:
$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
#4
5
str_split
can 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中的字符串可以像chars数组一样访问,在大多数情况下,您不需要将字符串分割为一个“新”数组。
#5
4
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()都是快速的,但与大字符串相比要慢得多。
#6
2
Try this:
试试这个:
$str = '546788';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
#7
2
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
)
#8
0
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);
#9
0
$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
因为在我的一些个人用途中,当有特殊字符的问题时,它被证明是更可靠的
#1
124
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");
str_split takes 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"
#2
19
What are you trying to accomplish? You can access characters in a string just like an array:
你想要完成什么?您可以访问字符串中的字符,就像数组一样:
$s = 'abcd';
echo $s[0];
prints 'a'
打印“a”
#3
6
Try this:
试试这个:
$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
#4
5
str_split
can 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中的字符串可以像chars数组一样访问,在大多数情况下,您不需要将字符串分割为一个“新”数组。
#5
4
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()都是快速的,但与大字符串相比要慢得多。
#6
2
Try this:
试试这个:
$str = '546788';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
#7
2
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
)
#8
0
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);
#9
0
$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
因为在我的一些个人用途中,当有特殊字符的问题时,它被证明是更可靠的