如何比较arabic-和假名全宽数字?

时间:2016-06-25 06:55:47

标签: php character-encoding

如何在PHP中比较两个字符串

県19−1県225−3県96−1

県19-1県225-3県96-1

第一个包含假名全宽数字,比较应该将它们视为等于阿拉伯数字。

1 个答案:

答案 0 :(得分:0)

您可以先使用zen-kaku (fullwidth)函数将所有han-kaku (halfwidth)字符转换为mb_convert_kana()个字符,然后进行比较,如下所示:

$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".

请注意,mb_convert_kana()位于 mbstring 扩展名中,但不是默认扩展名,因此您可能需要install it first