3

I'm reading from a file through 'file_get_contents'. After exploding the content, one of the elements is presented this way when I dump it:

dd($myVariable);

b"Crédito"

I've read on the internet that this is some kind of 'binary' string, related to a PHP version 6 that never existed. But I simply can not find a way to convert it to a 'regular' string.

I thought they were somehow equivalent, but I can't even use it to compare to another string. For example, none of these will ever return true:

if ($myVariable == "Crédito") 
if ($myVariable === "Crédito")
if ($myVariable == b"Crédito")
if ($myVariable == (binary)"Crédito")

How can I convert it to a regular string?

2
  • Is that output from var_dump? Commented May 19, 2018 at 0:47
  • b"Crédito" is not valid PHP, you'd need it to be 'b"Crédito"'. What does var_dump show, actually binary? Commented May 19, 2018 at 5:15

3 Answers 3

3

For me, utf8_encode helped. hope it will help someone else also

utf8_encode($string)
Sign up to request clarification or add additional context in comments.

Comments

1

You would need to unpack the binary data into a readable string using the unpack function (http://php.net/manual/en/function.unpack.php)

Example for a string:

$var = b"binary";
$unpacked = unpack("a*", $var); // "a*" stands for as much as NUL-padded strings as possible
var_dump($unpacked);

1 Comment

Doesn't work, string ist still marked as "b"
-1
mb_convert_encoding($text, 'UTF-8')

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.