note 76358 deleted from function.file-get-contents by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:39:20 +0000 |
| Subject: | note 76358 deleted from function.file-get-contents by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215251@lists.php.net to get a copy of this message | ||
Note Submitter: bearachute at gmail dot com
----
If you're having problems with binary and hex data:
I had a problem when trying to read information from a ttf, which is primarily hex data. A
binary-safe file read automatically replaces byte values with their corresponding ASCII characters,
so I thought that I could use the binary string when I needed readable ASCII strings, and bin2hex()
when I needed hex strings.
However, this became a problem when I tried to pass those ASCII strings into other functions (namely
gd functions). var_dump showed that a 5-character string contained 10 characters, but they
weren't visible. A binary-to-"normal" string conversion function didn't seem to
exist and I didn't want to have to convert every single character in hex using chr().
I used unpack with "c*" as the format flag to see what was going on, and found that every
other character was null data (ordinal 0). To solve it, I just did
str_replace(chr(0), "", $string);
which did the trick.
This took forever to figure out so I hope this helps people reading from hex data!