Why does a Sort method not work properly on LINUX servers?

Asked

Viewed 260 times

3

I have a question, I have a function to order a array which has names of accentuated persons (UTF-8). It works properly on WINDOWS servers, but when I launch the code to run on LINUX servers the accentuation gets interrogated and does not order correctly what will be ? I’ve consulted all the tutorials of "Sort words with accents in PHP", and everyone uses the method as I use it. I have tried usort uksort uasort none worked.

arrayMembros[$arrayDados2[0]] = array("idUsuario" => @$arrayDados2[0] , "nome" => ucwords(strtolower(utf8_encode(@$arrayDados2[1]))) , "sobrenome" => ucwords(strtolower(utf8_encode(@$arrayDados2[2])))    );

uasort(

    $arrayMembros,

     function( $a, $b ) {
        $variavela = iconv('UTF-8', 'ASCII//TRANSLIT', $a["nome"]);
        $variavelb = iconv('UTF-8', 'ASCII//TRANSLIT', $b["nome"]);

         return strcmp($variavela, $variavelb);
     }
);

Result on the LINUX server:

  • ?nio Lima
  • Alana Siqueira
  • Fulana da Silva

Expected result (and also result on the WINDOWS server):

  • Alana Siqueira
  • Ênio Lima
  • Fulana da Silva

Don’t ask me to remove the utf8_encode of the code, because I have already done it and it is the same, only instead of the pure interrogation, the result is the diamond interrogation. The data comes from text files and I want to display them directly on the page without having to insert them into a database and gives a command ORDER for the data to be ordered.

Code:

$url = file_get_contents(verificar()."GERAL_getUsuariosGrupo.asp?idGrupo='$idGrupo'");

    $arrayDados = explode(";;",$url);
    $arrayMembros = array();
    $arrayMembros2 = array();

    foreach($arrayDados as $dados){
        $arrayDados2 = explode("|",$dados);
        $arrayMembros[$arrayDados2[0]] = array("idUsuario" => @$arrayDados2[0] , "nome" => ucwords(strtolower(@$arrayDados2[1])) , "sobrenome" => ucwords(strtolower(@$arrayDados2[2]))    );
        }


        usort(

    $arrayMembros,

     function( $a, $b ) {
        $variavela = iconv('UTF-8', 'ASCII//TRANSLIT', $a["nome"]);
        $variavelb = iconv('UTF-8', 'ASCII//TRANSLIT', $b["nome"]);

         return strcmp($variavela, $variavelb);
     }
);

Ps.: Thanks in advance and I apologize for not being able to comment on the link I suggested above, because this policy of stackoverflow 50 reputation points to be able to comment forced me to open another discussion.

  • Try to use usort

  • @Wallacemaxters already tried "usort" "uksort" "uasort" and none worked.

  • 2

    this is a typical problem of poor configuration of the Encode charset.. vc can solve by implementing several gambiarras, but I advise to configure and fix charset of the environment.

  • @Danielomine alright ? Look, I did what you really indicated on the linux server was without the variable default_charset, was NULL. I corrected and put default_charset=UTF-8, I consulted using the function phpinfo() and is now standard UTF-8. However, even so the ordering does not occur correctly, although it has fixed the problem with the Bootstrap instead of the interrogation, now is appearing normally the name "Ênio Lima" except that in the upper part above Alana Siqueira.

  • 1

    Maicon, this just doesn’t solve it. It’s a very broad subject to explain to you in this short space of comments.. You don’t actually need to modify server settings, because in php you can set it at runtime.. But anyway, it depends on other factors, as for example, if the data are intact, ie without any kind of corruption. That is why it is difficult to give an exact answer. A more thorough diagnosis is required. I just advise you not to go around executing everything they tell you because then you risk corrupting data, making the situation worse.. for security, back up

  • Thanks @Danielomine. I am keeping the backups but my main concern is to resolve this situation, in C, Java I didn’t find many situations like these because they were desktop languages, and I never really needed to sort a string with array in PHP, as he used the commands ORDER from the database, but in this situation I receive data from text files and it would be ridiculous to have to insert this data into the database, read neatly and display the way I want, but while I can’t find a solution I think will be the output.

  • That really changes the context of your question.. I suggest that you add this last comment in the question itself because it is much clearer the objective and even easier to find solution.. by the way, I know how to solve..

  • @Danielomine what would be the suggested tests so that we can arrive at a reply?

  • there are many types of tests, but I would start by checking the type of data encounter.. in php there is a function called mb_detect_encode(). Read the manual to know how to use. http://php.net/mb_detect_encode

  • Please post the entire script so that we can run and see the problem. Where does it come from $arrayDados2? From what I’ve seen it seems a problem with utf8, but not in headers but rather as the file was saved, it may be another file on the server that is being used with include.

  • Managed to solve your problem?

  • The issue of appearing Ênio Lima, it worked, I took the data of an ASP page that writes only the names of users separated by ";", this ASP page was not coding utf-8, nor did it have headers. Then I populate the $arrayDados2, the problem now is that the "Ênio Lima" is at the top, ie does not order.

Show 7 more comments

1 answer

0

Are you running this code in the context of a web page? It may be that the page is not in UTF-8, so the strings may be received in ISO-8859-1 format, which even explains why the "Ê" was corrupted.

If you’re Apache, try using

AddDefaultCharset UTF-8

in the configuration, and check if the PHP page has the declaration

<meta http-equiv="Content-Type" content="text/html; charset="UTF-8">
  • Yes, I’m using it in the context you mentioned <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ... Can you tell me if Bootstrap changes any page encoding settings? Because I’m using it and apparently this is messing with the issue of "Ê" corrupted. But I did the test, I removed the Bootstrap page even though it did not correctly ordered the "Ênio Lima" stayed at the top.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.