NBT to PHP allows you to convert NBT data to PHP data types to easily use within a PHP program.
Since this program converts the NBT data to native PHP data types, there is currently no way to convert the data back to NBT data since PHP has fewer data types than NBT.
An advantage is having no PHP to NBT converter is this library is very lightweight and works very well in applications where read-only functionality is needed, like reading from an API or file.
An advantage of the approach of converting to PHP data types is that it is very easy to display and read the output using print_r.
This library can be downloaded using Composer. For example you can add the following to your composer.json file.
{
"require": {
"brandon/nbt": "*"
}
}It is recommended to replace * with a version constraint.
There are several ways to download directly from GitHub, the only required file is NBT.php.
NBT.php via https://raw.githubusercontent.com/BrandonXLF/NBT.php/master/NBT.phpgit clone https://github.com/BrandonXLF/NBT.phpAfter downloading, to use the library you would use:
require 'NBT.php';As of now, all functions in this library as static, meaning there's no need to create a new object of type NBT, but you can.
There are 3 main functions that you should access, NBT::readFile, NBT::readString, and NBT::readStream.
NBT::readFile is used to read NBT data from a file.
NBT::readFile( string $filename, string $wrapper = 'compress.zlib://' ) : arraycompress.zlib://. Change the wrapper to file:// to read a uncompressed file.An array with the NBT data converted to PHP.
NBT::readString is used to read NBT data from a string.
NBT::readString( string $str ) : arraygzdecode to decompress the string first.An array with the NBT data converted to PHP.
NBT::readStream is used to read NBT data from a file stream.
NBT::readStream( resource $stream ) : arrayAn array with the NBT data converted to PHP.
NBT::readTag is used to read a NBT tag from a file stream.
NBT::readTag( int $type, resource $stream ) : mixedThe NBT data read as PHP data, return type depends on the $type parameter.
Tests for this script can be found in the tests/test.php php file. You may run php tests/test.php to test this script.