How to compare Unicode fullwidth characters with normal characters?

610 views Asked by At

I have code that looks like:

<?php
$str1 = 'xxxxxID';
$str2 = 'xxxxxID';

$bool = ($str1 == $str2);
var_dump( $bool);//==> need return true.
?>

Please tell me, how to compare those strings?

3

There are 3 answers

0
BreyndotEchse On BEST ANSWER

You are looking for transliteration. You can use iconv:

<?php
$str1 = 'xxxxxID';
$str2 = 'xxxxxID';

$str1Translit = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $str1);
$str2Translit = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $str2);

$bool = ($str1Translit == $str2Translit);
var_dump( $bool);//==> need return true.

But you should know, that this does not work for every unicode character and may show some odd results.

1
axiac On

Use mb_convert_encoding() to bring both strings to the same multi-byte encoding (utf-8 or the one of $str2) then you can compare them as usual, using ==

2
AudioBubble On

strlen(); Will return the number of bytes in a string. Your question is somewhat unclear, however.