PHP says that two equal strings have different lengths

Asked

Viewed 230 times

5

I want to compare two strings but something unusual happens. I have the code:

<?php
$char = 'Á';

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

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

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

Exactly equal values but with different sizes. Any idea why?

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 issue is unrelated to UTF encoding, in the same way that native functions for string comparison present difference with acentos or special char.

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


It’s the way PHP treats acentos or special char.
var_dump servre only to return visual information about an element and is liable to conflict in the interpretation of such characters.

Browser other questions tagged

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