How to sort the words in a string with PHP

1 Answer

0 votes
function sortTheWordsInAString($s) {
    $arr = explode(" ", $s);
    sort($arr);
    $result = implode(" ", $arr);
    
    return $result;
}

$s = "php c java c++ python c#";

$s = sortTheWordsInAString($s);

echo $s;



/*
run:

c c# c++ java php python

*/

 



answered Jul 29, 2024 by avibootz
...