Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将字符串转换为按字母分组_Php - Fatal编程技术网

Php 将字符串转换为按字母分组

Php 将字符串转换为按字母分组,php,Php,如何创建将字符串转换为另一个按字母或数字分组的字符串的正则表达式: $string = "cucumber"; 正则表达式后:ccumber $string = "tohothin"; 正则表达式之后:ttoohhin 我如何用PHP创建它?这对于正则表达式并不重要,它可能是另一个函数。好吧,我没有看到尝试,但我很无聊: $result = ''; foreach(array_count_values(str_split($string)) as $letter => $count)

如何创建将字符串转换为另一个按字母或数字分组的字符串的正则表达式:

$string = "cucumber";
正则表达式后:
ccumber

$string = "tohothin";
正则表达式之后:
ttoohhin


我如何用PHP创建它?这对于正则表达式并不重要,它可能是另一个函数。

好吧,我没有看到尝试,但我很无聊:

$result = '';

foreach(array_count_values(str_split($string)) as $letter => $count) {
    $result .= str_repeat($letter, $count);
}
产量:
ccumber

$string = "tohothin";
排序将起作用,但给出不同的顺序:

$letters = str_split($string);
sort($letters);
$result = implode('', $letters);

收益率:
bccemruu

嗯,我没有看到有人尝试,但我很无聊:

$result = '';

foreach(array_count_values(str_split($string)) as $letter => $count) {
    $result .= str_repeat($letter, $count);
}
产量:
ccumber

$string = "tohothin";
排序将起作用,但给出不同的顺序:

$letters = str_split($string);
sort($letters);
$result = implode('', $letters);

收益率:
bccemruu

结果中的字母顺序必须是什么?是按字母顺序还是按外观顺序?这并不重要,但按字母顺序更合适。结果中的字母顺序必须是什么?是按字母顺序还是按外观顺序?这并不重要,但字母顺序更合适。