2

I have read, that b in front of something means binary.

I am receiving a text field from MS DB, column type is CLOB. I am using Laravel and when I die dump (dd()) I see:

b"""
My big text
"""

If I create simple string and dd() it I see:

"My big text"

The problem is that json_encode() returns false on this b-String, but everything fine with simple string.

Could you please tell me how can I make it a simple string?

P.S. I have tried unpack() -> unsuccess

EDIT: actually json_encode() not related to this binary string. It was failing cause of non utf8 symbol. I see ...(22:45 – 0:15 CEST)..., but when I do utf8_decode($text), I see ...(22:45 ? 0:15 CEST)... and if I try to json_encode() now, it's works perfect.

Aleksandrs
  • 1,488
  • 1
  • 14
  • 34
  • Can you check the schema for the actual data type of this column please and add that to your question – RiggsFolly Jul 26 '18 at 10:39
  • Try to unpack the string first with unpack function http://php.net/manual/en/function.unpack.php – Ylber Veliu Jul 26 '18 at 10:43
  • If that was truely binary data I dont think you would get something as readable as what you are getting – RiggsFolly Jul 26 '18 at 10:44
  • @YlberVeliu, I have already tried, I wrote it. I can try 1 more time, which format should I use? – Aleksandrs Jul 26 '18 at 10:46
  • @RiggsFolly I see my string like usual string except it has b""" instead of " and """ instead of " at the end – Aleksandrs Jul 26 '18 at 10:47
  • So please can you show us the schema for this table – RiggsFolly Jul 26 '18 at 10:47
  • @RiggsFolly yes, I were waiting answer from other people, it's not my db. Data type is: CLOB – Aleksandrs Jul 26 '18 at 10:49
  • 1
    I have seen this weird `b""` string in relation to Laravel several times now, and no clear answer what it is. The fact is that PHP doesn't have two types of strings. There's only one string type and it's always "binary". The `b` might just be something that `dd` adds for whatever reason. What does `var_dump` on the same value give you? – deceze Jul 26 '18 at 11:07
  • string(1038) "My Text ... � ..." And what about this � issue I have described it in EDIT: section – Aleksandrs Jul 26 '18 at 11:11
  • Forget unpack etc. JUST MAKE ltrim(b"), rtrim("")... Happy coding. – Nevermore Jul 26 '18 at 11:11
  • 1
    @Nevermore `b` is not part of the string itself, it's just debug output. – deceze Jul 26 '18 at 11:15

1 Answers1

2

PHP does not have "binary" and "non-binary" strings. It just has strings, and they're always "binary", as they're just acting like byte arrays. The b prefix is added by the Symfony VarDumper component as a sign that the string is not valid UTF-8. Arguably UTF-8 should be the one and only sensible encoding in use today, and apparently Symfony goes so far as to declare anything else as "binary", i.e. atypical text.

That is also the reason why your json_encode failed.

FWIW, b was a proposed forward compatibility prefix to prepare PHP code for PHP 6, which was supposed to have very Python-like binary strings and Unicode strings. Only PHP 6 never happened and the b prefix still does nothing. Symfony seems to have gone all out and adopted the b and """ conventions from Python nonetheless.

deceze
  • 510,633
  • 85
  • 743
  • 889