Kẻ Thầm Lặng Kẻ Thầm Lặng - 3 months ago 14x
PHP Question

How to compare arabic- and kana full-width digits?

How can I compare in PHP the two strings





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 han-kaku (halfwidth) first using mb_convert_kana(), 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.

$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 now that they are the same.

Note that mb_convert_kana() is in the mbstring extension which is not a default installation, so you may have to install it first.