Why are below code echos not equal even if both strings are equal?
$number = 1234567.89;
$expected = 'GBP 1,234,567.89';
$fmt = new NumberFormatter('en_AU@currency=GBP', NumberFormatter::CURRENCY);
$currency = $fmt->formatCurrency($number, 'GBP');
echo "$expected = $currency ?" . PHP_EOL;
echo $expected == $currency ? 'equals' : "not equal";
Output
GBP 1,234,567.89 = GBP 1,234,567.89 ?
not equal
Any help will be really helpful, thanks
CodePudding user response:
Because $expected
variable has a whitespace after "GBP"
$expected = 'GBP 1,234,567.89';
does not equal to 'GBP1,234,567.89';
actually you can debug them;
echo $expected;
echo "<br>";
echo $currency;
CodePudding user response:
Okay trying a few things I got the answer. The $exoected and $currency has a different encoding.
print_r([mb_detect_encoding($expected), mb_detect_encoding($currency)]);
Array
(
[0] => ASCII
[1] => UTF-8
)
The space character in $currency is 194 ASCII number whereas space character in $expected is 32
if (preg_match('/[^a-zA-Z0-9 ] /', $number, $matches)) {
print_r(ord($matches[0]));
print_r(ord(' '));
}
Hope this helps someone who scratching their head