I want to compare two string but one is ASCII encoded and the other is UTF-8 encoded.
I try to convert the ASCII string to UTF-8 with utf8_encode
, or iconv
or mb_convert_encoding
but it does not work. It is still in ASCII format.
What is strange it is that the ASCII string is defined in my php code file (not a string coming from a file or database). When there is an accent in the string, then it is utf-8 encoded
Here is an example of my php file
$return = array(
array('name' => 'Date'), // => ASCII
array('name' => 'État') // => utf-8
)
And here is my string comparison
// $line is coming from a file with 'fopen'. It is equal to 'Date'
$stringUtf8 = iconv(mb_detect_encoding($line), 'UTF-8//IGNORE', $line); // => UTF-8
$foundHeader = "Date"; // => ASCII
if($foundHeader == $stringUtf8){
echo "foo";
}
It never echoes "foo" :(
And here is why I dump
dd([
$foundHeader,
strlen($foundHeader),
mb_detect_encoding($foundHeader),
$stringUtf8,
strlen($stringUtf8),
mb_detect_encoding($stringUtf8),
]);
& the result
array:6 [
0 => "Date"
1 => 4
2 => "ASCII"
3 => "Date"
4 => 7
5 => "UTF-8"
]
And here it is how I get filecontent
$handle = fopen($fichier->getPathname(), 'r');
while ($line = fgets($handle)){
...// do stuffs
}