- array_map: documentation ( source)
- implode: documentation ( source)
- str_replace: documentation ( source)
- strtoupper: documentation ( source)
- dechex: documentation ( source)
- str_split: documentation ( source)
- ord: documentation ( source)
<?php
class Guid
{
private $guid = '';
public function __construct($guid)
{
$this->guid = $guid;
}
public static function newGuid()
{
return new Guid('35918bc9-196d-40ea-9779-889d79b753f0');
}
public function __toString()
{
return $this->guid;
}
public function toByteArray()
{
$str = str_replace('-', '', $this->guid);
$ints = array_map('hexdec', str_split($str, 2));
// swap
$newints = $ints;
$newints[0] = $ints[3];
$newints[1] = $ints[2];
$newints[2] = $ints[1];
$newints[3] = $ints[0];
$newints[4] = $ints[5];
$newints[5] = $ints[4];
$newints[6] = $ints[7];
$newints[7] = $ints[6];
return implode('', array_map('chr', $newints));
}
public function printBytes($bytes)
{
for ($i = 0; $i < strlen($bytes); $i++) {
echo strtoupper(dechex(ord($bytes[$i]))).' ';
}
echo "\n";
}
}
$guid = Guid::newGuid();
$str = $guid->toByteArray();
$guid->printBytes($str);