1
votes

I'm importing a large file in php, with PHP 7.2.10, but there is some strings that have "b" (binary) prefix, like:

$str = b"PRAÇA";

When that string are saved in mongodb, it throws an exception:

Detected invalid UTF-8 for field path "$set.field": PRA�A

If I run mb_detect_encoding($str), it returns "UTF-8".

And if I run iconv(mb_detect_encoding($str), "UTF-8//IGNORE", $str), it returns "PRAA". Yes I know that "//IGNORE" will ignore non-utf8 characters.

What can I do to return string PRAÇA?

I really need to be that string.

Thanks.

1

1 Answers

0
votes

You can using something like this:

function binaryToString($binary)
{
    $binaries = explode(' ', $binary);
 
    $string = null;
    foreach ($binaries as $binary) {
        $string .= pack('H*', dechex(bindec($binary)));
    }
 
    return $string;    
}