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?

faintsignal
  • 1,828
  • 3
  • 22
  • 30
gabpalves
  • 31
  • 1
  • 5

3 Answers3

2

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

utf8_encode($string)
Emtiaz Zahid
  • 2,645
  • 2
  • 19
  • 35
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);
Gregoire Lodi
  • 557
  • 2
  • 10
-1
mb_convert_encoding($text, 'UTF-8')
Anas Alpure
  • 510
  • 5
  • 9