我需要将一个字符串拆分成一个单字符字符串数组,并获得拆分字符的计数。
分裂“字符”将给数组"c", "h", "a", "r", "a", "c", "t", "e", "r"。
编辑
是否可以使用内置函数获得计数分裂的字符串字符?
Array ( [c] => 2 [h] => 1 [a] => 2 [r] => 2 [t] => 1 [e] => 1 ) 发布于 2013-02-09 14:36:15
使用str_split
$array = str_split("cat");试试count_chars
<?php
$data = "Two Ts and one F.";
foreach (count_chars($data, 1) as $i => $val) {
echo "There were $val instance(s) of \"" , chr($i) , "\" in the string.\n";
}
?>上面的示例将输出:
There were 4 instance(s) of " " in the string.
There were 1 instance(s) of "." in the string.
There were 1 instance(s) of "F" in the string.
There were 2 instance(s) of "T" in the string.
There were 1 instance(s) of "a" in the string.
There were 1 instance(s) of "d" in the string.
There were 1 instance(s) of "e" in the string.
There were 2 instance(s) of "n" in the string.
There were 2 instance(s) of "o" in the string.
There were 1 instance(s) of "s" in the string.
There were 1 instance(s) of "w" in the string.发布于 2013-02-09 14:36:38
[ $array ]
$array = str_split('Cat');与str_split() 拆分后的将如下所示:
ARRAY
{
[0] = 'C'
[1] = 'a'
[2] = 't'
}对编辑问题的回答
是的,您可以使用函数count_chars()
$str = "CHARACTERS";
$array = array();
foreach (count_chars($str, 1) as $i => $val) {
array[] = array($str, $i);
}将输出以下内容:
ARRAY
{
[0] = ARRAY("C" => 2)
[1] = ARRAY("H" => 1)
}等
发布于 2013-02-09 14:35:28
使用php函数拆分,这里的示例如下:
$array = str_split("cat");https://stackoverflow.com/questions/14789153
复制相似问题