PHP says that two equal strings have different lengths

5

I'm trying to compare two strings, but something unusual happens. I have the code:

<?php
$char = 'Á';

var_dump('Á');          
var_dump($char);

The variable $char receives 'Á' and the result is as follows.

string 'Ã' (length=2)
string 'Ã' (length=1)

Exactly the same values but with different sizes. Any idea why this?

    
asked by anonymous 21.07.2014 / 10:08

1 answer

11

If you want to compare strings that have acentos or special char , the best way is mb _ *
mb_strlen to count the number of characters

  

The question has no relation to UTF coding, just as the native functions for string comparison have a difference with acentos or special char .

strlen( 'aviao' ) // 5
strlen( 'avião' ) // 6
mb_strlen( 'avião' , 'utf-8' ) // 5


This is how PHP treats acentos or special char . var_dump serves only to return visual information about an element and is liable to conflict in the interpretation of such characters.

    
21.07.2014 / 10:28