2

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 Answer 1

0

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;    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.