PHP:如何对字符串中的字符进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9912469/
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: How to sort the characters in a string?
提问by Jér?me Verstrynge
I have a set of strings containing characters in a PHP script, I need to sort those characters in each string.
我有一组包含 PHP 脚本中字符的字符串,我需要对每个字符串中的这些字符进行排序。
For example:
例如:
"bac" -> "abc"
"abc" -> "abc"
"" -> ""
"poeh" -> "ehop"
These characters don't have accents and are all lower case. How can I perform this in PHP?
这些字符没有重音,都是小写的。如何在 PHP 中执行此操作?
回答by Mike B
I would make it an array and use the sort function:
我会把它变成一个数组并使用 sort 函数:
$string = 'bac';
$stringParts = str_split($string);
sort($stringParts);
echo implode('', $stringParts); // abc
回答by James Cordeiro
When using sort, it needs to be lowercase text. I had upper case strings cause many issues on my extremely string heavy sitewith asort() and sort() not knowing this ;). I use the following function for a quick script. Note the "strtolower" for the allChars function. You can then manipulate however you need later on with lowercase. Or, the other string ordering which handles upper case and lower case, is natcasesort(). Natcasesort which handles proper (natural) ordering - in the way we would order things on paper. You could provide an array, and use a foreach to separate each word. Or, use this is a base for creating a function that handles arrays. You could use implode('',$letters) instead of a foreach statement - but this function allows you to alter the letters if you need to - just do so inside the foreach. Also added implode functions in the case someone prefers those.
使用排序时,需要为小写文本。我的大写字符串在我的非常重的字符串网站上导致了许多问题,而 asort() 和 sort() 不知道这一点;)。我将以下函数用于快速脚本。注意 allChars 函数的“strtolower”。然后,您可以稍后使用小写字母进行操作。或者,另一个处理大写和小写的字符串排序是natcasesort(). Natcasesort 处理正确(自然)排序——就像我们在纸上排序一样。您可以提供一个数组,并使用 foreach 来分隔每个单词。或者,使用 this 作为基础来创建处理数组的函数。您可以使用 implode('',$letters) 而不是 foreach 语句 - 但此函数允许您在需要时更改字母 - 只需在 foreach 内进行即可。还添加了内爆功能,以防有人喜欢这些功能。
Lowercase Only
仅小写
function allChars($w){
$letters = str_split(strtolower($w)); sort($letters);
$ret = "";
foreach($letters as $letter){
$ret .= $letter;
}
return $ret;
}
Lowercase Only With Implode
小写仅与内爆
function implodeAllChars($w){
$letters = str_split(strtolower($w)); sort($letters);
return implode('',$letters);
}
Natural Ordering Function
自然排序函数
function allCharsNat($w){
$letters = str_split($w); natcasesort($letters);
$ret = "";
foreach($letters as $letter){
$ret .= $letter;
}
return $ret;
}
Natural Ordering with Implode
内爆的自然排序
function allCharsNatImplode($w){
$letters = str_split($w); natcasesort($letters);
return implode('',$letters);
}
It's quick and simple.
它快速而简单。
回答by dan-lee
function sort_alphabet($str) {
$array = array();
for($i = 0; $i < strlen($str); $i++) {
$array[] = $str{$i};
}
// alternatively $array = str_split($str);
// forgot about this
sort($array);
return implode('', $array);
}
回答by Nilpo
You can split the string into an array and then use any of the various sorting functions.
您可以将字符串拆分为一个数组,然后使用各种排序函数中的任何一个。
$in = "this is a test";
$chars = str_split($in);
sort($chars);
$out = implode($chars);
回答by LennartF22
Keep in mind that str_split() does not support multibyte strings. In case you have to deal with e.g. UTF-8 strings, preg_split() should be used instead:
请记住, str_split() 不支持多字节字符串。如果您必须处理例如 UTF-8 字符串,则应改用 preg_split():
$string = 'bac';
$stringParts = preg_split('//u', $string);
sort($stringParts);
echo implode('', $stringParts); // abc