How can I compare in PHP the two strings
県19−1県225−3県96−1
and
県19-1県225-3県96-1
?
The first one contains kana full-width numbers, the comparison should treat them as equal to the arabic-numeral.
How can I compare in PHP the two strings
県19−1県225−3県96−1
and
県19-1県225-3県96-1
?
The first one contains kana full-width numbers, the comparison should treat them as equal to the arabic-numeral.
You can convert all the zen-kaku (fullwidth) characters to their han-kaku (halfwidth) equivalents first using the mb_convert_kana()
function, and then do the comparison, like this:
$string1 = "県19−1県225−3県96−1";
$string2 = "県19-1県225-3県96-1";
var_dump($string1 === $string2); // This is false because they are different.
// Convert fullwidth to halfwidth.
$string1 = mb_convert_kana($string1, "a"); // Specify "a" for converting both alphabets
$string2 = mb_convert_kana($string2, "a"); // and numbers. See doc for other options.
var_dump($string1 === $string2); // This becomes true, both are "県19-1県225-3県96-1".
Note that mb_convert_kana()
is in the mbstring extension which is not a default extension, so you may have to install it first.