arrays - How can I sort words of Unicode sequence in php?

522

Example:

'lemon orange banana apple' ---> 'elmno aegnor aaabnn aelpp'
'лимон апельсин банан яблоко' ---> 'илмно аеилнпсь аабнн бклооя'
'αβγαβγ αβγαβγαβγ' ---> 'ααββγγ αααβββγγγ'

857

Answer

Solution:

function array_sorting($string) {

// Split string sequence into array elements
$string = explode(" ", $string);
//print_r($string);

foreach($string as $str) {

    // Converting words into array elements
    $results = array();
    preg_match_all('/./u', $str, $results);

    foreach ( $results as $array ) {

        // Sorting array elements
        sort($array);

        // Concat elements back to words
        foreach($array as $val){
            echo $val;
        }
    }
    // Concat words back to sequence
    echo " ";
}

}

array_sorting('αβγαβγ αβγαβγαβγ');

People are also looking for solutions to the problem: php - Api-platform filter annotation for Many To Many

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.