mirror of
https://github.com/danog/PHPStruct.git
synced 2024-11-26 19:54:38 +01:00
113 lines
5.6 KiB
HTML
113 lines
5.6 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en-us">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>PHPStruct by danog</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<link rel="stylesheet" type="text/css" href="stylesheets/normalize.css" media="screen">
|
|
<link href='https://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css'>
|
|
<link rel="stylesheet" type="text/css" href="stylesheets/stylesheet.css" media="screen">
|
|
<link rel="stylesheet" type="text/css" href="stylesheets/github-light.css" media="screen">
|
|
</head>
|
|
<body>
|
|
<section class="page-header">
|
|
<h1 class="project-name">PHPStruct</h1>
|
|
<h2 class="project-tagline"> PHP implementation of Python's struct module.</h2>
|
|
<a href="https://github.com/danog/PHPStruct" class="btn">View on GitHub</a>
|
|
<a href="https://github.com/danog/PHPStruct/zipball/master" class="btn">Download .zip</a>
|
|
<a href="https://github.com/danog/PHPStruct/tarball/master" class="btn">Download .tar.gz</a>
|
|
</section>
|
|
|
|
<section class="main-content">
|
|
<h1>
|
|
<a id="phpstruct-class" class="anchor" href="#phpstruct-class" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>PHPStruct class</h1>
|
|
|
|
<p><a href="https://travis-ci.org/danog/PHPStruct"><img src="https://travis-ci.org/danog/PHPStruct.svg?branch=master" alt="Build Status"></a>
|
|
<a href="https://www.codacy.com/app/daniil-gentili-dg/PHPStruct?utm_source=github.com&amp;utm_medium=referral&amp;utm_content=danog/PHPStruct&amp;utm_campaign=Badge_Grade"><img src="https://api.codacy.com/project/badge/Grade/7b91e30ec89a4313bdb34766ea990113" alt="Codacy Badge"></a>
|
|
<a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/packagist/l/danog/phpstruct.svg?maxAge=2592000?style=flat-square" alt="License"></a>
|
|
<a href="https://packagist.org/packages/danog/phpstruct"><img src="https://img.shields.io/packagist/dm/danog/phpstruct.svg?maxAge=2592000?style=flat-square" alt="Packagist download count"></a>
|
|
<a href="https://packagist.org/packages/danog/phpstruct"><img src="https://img.shields.io/packagist/v/danog/PHPStruct.svg?maxAge=2592000?style=flat-square" alt="Packagist"></a>
|
|
<a href="http://hhvm.h4cc.de/package/danog/phpstruct"><img src="http://hhvm.h4cc.de/badge/danog/phpstruct.svg?style=flat-square" alt="HHVM Status"></a>
|
|
<a href="https://styleci.io/repos/62454134"><img src="https://styleci.io/repos/62454134/shield" alt="StyleCI"></a></p>
|
|
|
|
<p>Licensed under MIT.</p>
|
|
|
|
<p>PHP implementation of Python's struct module.</p>
|
|
|
|
<p>This library was created to help me develop a <a href="https://github.com/danog/MadelineProto">client for the mtproto protocol</a>.<br>
|
|
It supports php 5.6, php 7 and HHVM. </p>
|
|
|
|
<p>The functions and the formats are exactly the ones used in python's struct
|
|
(<a href="https://docs.python.org/3/library/struct.html">https://docs.python.org/3/library/struct.html</a>)</p>
|
|
|
|
<p>This library can be used to pack/unpack strings, ints, floats, chars and bools into bytes.
|
|
It has lots of advantages over PHP's native implementation of pack and unpack, such as: </p>
|
|
|
|
<ul>
|
|
<li>Custom byte endianness.</li>
|
|
<li>Lots of useful formats that aren't present in the native implementation.</li>
|
|
<li>The syntax of the format string of pack and unpack is the same as in python's struct module.</li>
|
|
<li>The result of unpack is normal numerically indexed array that starts from 0 like it should.</li>
|
|
<li>The result of unpack has type casted values (int for integer formats, bool for boolean formats, float for float formats and string for all of the other formats).</li>
|
|
<li>The calcsize function is implemented.</li>
|
|
<li>The q and Q formats can be used even on 32 bit systems (the downside is limited precision).</li>
|
|
<li>Padding is supported for the @ modifier.</li>
|
|
</ul>
|
|
|
|
<p>For now custom byte size may not work properly on certain machines for the f and d formats.</p>
|
|
|
|
<h2>
|
|
<a id="installation" class="anchor" href="#installation" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Installation</h2>
|
|
|
|
<p>Install using composer:</p>
|
|
|
|
<pre><code>composer require danog/phpstruct
|
|
</code></pre>
|
|
|
|
<h1>
|
|
<a id="usage" class="anchor" href="#usage" aria-hidden="true"><span aria-hidden="true" class="octicon octicon-link"></span></a>Usage</h1>
|
|
|
|
<p>Dynamic (recommended) </p>
|
|
|
|
<pre><code>require('vendor/autoload.php');
|
|
$struct = new \danog\PHP\StructClass();
|
|
$pack = $struct->pack("2cxi", "ab", 44);
|
|
$unpack = $struct->unpack("2cxi", $pack);
|
|
var_dump($unpack);
|
|
$count = $struct->calcsize("2cxi");
|
|
</code></pre>
|
|
|
|
<p>Dynamic (while specifying format string during istantiation) </p>
|
|
|
|
<pre><code>require('vendor/autoload.php');
|
|
$struct = new \danog\PHP\StructClass("2cxi");
|
|
$pack = $struct->pack("ab", 44);
|
|
$unpack = $struct->unpack($pack);
|
|
var_dump($unpack);
|
|
$count = $struct->size;
|
|
$formatstring = $struct->format;
|
|
</code></pre>
|
|
|
|
<p>Static</p>
|
|
|
|
<pre><code>require('vendor/autoload.php');
|
|
$pack = \danog\PHP\Struct::pack("2cxi", "ab", 44);
|
|
$unpack = \danog\PHP\Struct::unpack("2cxi", $pack);
|
|
var_dump($unpack);
|
|
$count = \danog\PHP\Struct::calcsize("2cxi");
|
|
</code></pre>
|
|
|
|
<p><a href="http://daniil.it">Daniil Gentili</a></p>
|
|
|
|
<footer class="site-footer">
|
|
<span class="site-footer-owner"><a href="https://github.com/danog/PHPStruct">PHPStruct</a> is maintained by <a href="https://github.com/danog">danog</a>.</span>
|
|
|
|
<span class="site-footer-credits">This page was generated by <a href="https://pages.github.com">GitHub Pages</a> using the <a href="https://github.com/jasonlong/cayman-theme">Cayman theme</a> by <a href="https://twitter.com/jasonlong">Jason Long</a>.</span>
|
|
</footer>
|
|
|
|
</section>
|
|
|
|
|
|
</body>
|
|
</html>
|