PHP:如何对字符串中的字符进行排序? [英] PHP: How to sort the characters in a string?

查看:60
本文介绍了PHP:如何对字符串中的字符进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组包含 PHP 脚本中字符的字符串,我需要对每个字符串中的这些字符进行排序.

I have a set of strings containing characters in a PHP script, I need to sort those characters in each string.

例如:

"bac" -> "abc"
"abc" -> "abc"
"" -> ""
"poeh" -> "ehop"

这些字符没有重音,都是小写的.如何在 PHP 中执行此操作?

These characters don't have accents and are all lower case. How can I perform this in PHP?

推荐答案

我会把它做成一个数组并使用排序功能:

I would make it an array and use the sort function:

$string = 'bac';

$stringParts = str_split($string);
sort($stringParts);
echo implode($stringParts); // abc

这篇关于PHP:如何对字符串中的字符进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆