Search code examples
phpunicodecomparemultibyte

How to compare Unicode fullwidth characters with normal characters?


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?


Solution

  • 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.