Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Sameer Borate:
Unpacking binary data in PHP
Apr 04, 2018 @ 14:19:40

Sameer Borate has a new post to his Code Deisel blog showing how to unpack binary data in PHP making use of the pack and unpack functions.

Working with binary files in PHP is rarely a requirement. However when needed the PHP ‘pack’ and ‘unpack’ functions can help you tremendously. To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension the file may have. We are not to use any GD library functions.

He starts by briefly covering the "metadata" that binary files contain in their headers about the type of file and, more specifically, what the content for a GIF looks like. He follows this with an example of using unpack to provide a readable version of the file's binary content. He then uses this in an is_gif function to read the contents of a file, check the header format and ensure the version is either GIF87a or GIF89a. He also includes the code for another method to get attributes about the file like height, width and the aspect ratio. He ends the post with more details on the contents of the header and what each character means.

tagged: binary data unpack pack tutorial gif image

Link: https://www.codediesel.com/php/unpacking-binary-data/

Igor Wiedler:
Binary parsing with PHP
Sep 25, 2012 @ 17:17:41

Igor Wiedler has a new post to his blog showing how to work with binary data in your PHP applications a few different built-in functions including unpack and bindec.

Binary operations in PHP are a bit strange. Since PHP was originally a templating layer for C code, it still has many of those C-isms. Lots of the function names map directly to C-level APIs, even if they work a bit differently sometimes. For example, PHP's strlen maps directly to STRLEN(3), and there are countless examples of this. However, as soon as it comes to dealing with binary data, things suddenly become very different.

He starts off looking at "the C way" to unpack a string (getting the ASCII values of each character) and shows how *not* to do it in PHP with ord. Instead he uses "unpack", bitwise operators and bindec to work with the actual binary data of the string.

tagged: binary parse ord unpack tutorial bindec bitwise

Link:

PHPMaster.com:
5 Inspiring (and Useful) PHP Snippets
Jul 02, 2012 @ 15:58:45

On PHPMaster.com there's a new tutorial that shares some useful PHP snippets that you could use in your development.

"X PHP Snippets" type articles abound on the Internet, so why write another one? Well, let's face it… the PHP snippets in them are generally lame. Snippets that generating a random string or return $_SERVER["REMOTE_ADDR"] for the IP Address of a client really aren't that interesting and are of modest usefulness. Instead, here's five snippets that you'll no doubt find interesting and useful, presented along with the problems that inspired them. I hope the creativity in many of them inspire you to write better and more creative code in your own day-to-day endeavors.

Their "five tips" are about:

  • Generating CSV files from an array of data
  • Autoloading classes (in a PSR-0 way)
  • Parsing data with the unpack function
  • Templating in HTML (creating a "View" object)
  • Using file_get_contents as a cURL Alternative
tagged: snippets csv autoload unpack template filegetcontents

Link:

Sameer Borate's Blog:
Unpacking binary data in PHP
Sep 22, 2010 @ 15:45:52

Sameer Borate has a new post to his blog today talking about a method for unpacking binary data directly in PHP - specifically in working with images.

To set the stage we will start with a programming problem, this will keep the discussion anchored to a relevant context. The problem is this : We want to write a function that takes a image file as an argument and tells us whether the file is a GIF image; irrelevant with whatever the extension the file may have. We are not to use any GD library functions.

He shows how to use the unpack function to open up the file and pull out the raw data - including the header information that tells you what kind of file it is you're working with. There's also an example of unpacking the header contents and grabbing things like height, width and the aspect of the image.

tagged: unpack binary data tutorial gif image

Link:


Trending Topics: