28

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

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?

Jérôme Verstrynge
  • 57,710
  • 92
  • 283
  • 453

5 Answers5

52

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

$string = 'bac';

$stringParts = str_split($string);
sort($stringParts);
echo implode($stringParts); // abc
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Mike B
  • 31,886
  • 13
  • 87
  • 111
5

When using sort, it needs to be lowercase text. I had upper case strings cause many issues on my extremely string heavy site with 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.

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.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
James Cordeiro
  • 521
  • 1
  • 5
  • 8
3
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);
}
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
dan-lee
  • 14,365
  • 5
  • 52
  • 77
2

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);
Nilpo
  • 4,675
  • 1
  • 25
  • 39
1

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:

$string = 'bac';
$stringParts = preg_split('//u', $string);
sort($stringParts);
echo implode($stringParts); // abc
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
LennartF22
  • 61
  • 8