Search code examples
phpiconv

iconv not working when dynamic variable is used


I am using following code to convert spanish characters to normal English characters:

function test_enc($text)
{
    setlocale(LC_ALL, 'en_US.utf8');
    return iconv('utf8', 'ascii//TRANSLIT', $text);
}
echo test_enc('TéstFirst'); returns TestFirst

and it is working fine, but when I am passing this argument dynamically from other array having same value then it is returning nothing like:

echo test_enc($data['firstname']);

I have used var_dump to see the difference and it returned following:

var_dump('TéstFirst');//returned string(10) "TéstFirst"
var_dump($data['travelername']);// returned string(9) "TéstFirst"

Please let me know what I am doing wrong.


Solution

  • According to the output you provided from var_dump(bin2hex($data['firstname'])) which is string(18) "54e973744669727374", this string is not valid UTF-8. As such if you try to convert it from utf8 to anything else in iconv it will give you an error telling you it's not valid utf8 and return false.

    var_dump(iconv('utf8', 'ascii//TRANSLIT', hex2bin("54e973744669727374")));
    

    This gives you

    PHP Notice:  iconv(): Detected an illegal character in input string in ... on line ...
    bool(false)
    

    What we could try to do is first attempt to convert this string into valid UTF-8 and then use iconv TRANSLIT. Right now we're just telling iconv that it's already valid utf8, which it's clearly not.

    var_dump(utf8_encode(hex2bin("54e973744669727374"))); //string(10) "TéstFirst"
    
    //string(9) "TestFirst"
    var_dump(iconv('utf8', 'ascii//TRANSLIT', utf8_encode(hex2bin("54e973744669727374"))));
    

    It might be better to just consult the documentation for the API you're using and figure out what character encoding they are sending the data in to safely convert from that to utf8.