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
– Wallace Maxters
@Wallacemaxters already tried "usort" "uksort" "uasort" and none worked.
– Maicon Herverton
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.
– Daniel Omine
@Danielomine alright ? Look, I did what you really indicated on the linux server was without the variable
default_charset
, was NULL. I corrected and putdefault_charset=UTF-8
, I consulted using the functionphpinfo()
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.– Maicon Herverton
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
– Daniel Omine
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 witharray
inPHP
, as he used the commandsORDER
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.– Maicon Herverton
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..
– Daniel Omine
@Danielomine what would be the suggested tests so that we can arrive at a reply?
– Maicon Herverton
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
– Daniel Omine
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 withutf8
, but not in headers but rather as the file was saved, it may be another file on the server that is being used withinclude
.– Guilherme Nascimento
Managed to solve your problem?
– durtto
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.– Maicon Herverton