无法将“二进制"字符串转换为“常规"字符串 [英] Can't convert 'binary' string to 'regular' string

查看:51
本文介绍了无法将“二进制"字符串转换为“常规"字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过"file_get_contents"读取文件.分解内容后,当我转储内容时,将以这种方式显示其中一个元素:

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);

bCrédito"

b"Crédito"

我在互联网上读到,这是某种二进制"字符串,与从未存在过的PHP版本6有关.但是我根本找不到将其转换为常规"字符串的方法.

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.

我以为它们在某种程度上是等效的,但是我什至不能用它来与另一个字符串进行比较.例如,这些都不会返回true:

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?

推荐答案

您需要使用 unpack 函数(字符串示例:

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

这篇关于无法将“二进制"字符串转换为“常规"字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆