3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php method_exists('SplFileObject', 'fread') or die("No SplFileObject::fread()"); $width = 16; $hex_width = 3 * $width; $file = new SplFileObject(__FILE__); while ($file->valid()) { $chunk = $file->fread($width); $hex = array_map(function ($char) { return sprintf('%02X', ord($char)); }, str_split($chunk)); printf("%-' {$hex_width}s %s\n", implode(' ', $hex), preg_replace('/[^[:print:]]/', '.', $chunk)); }
based on Q9vDP
Output for 5.5.24 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
3C 3F 70 68 70 0A 0A 6D 65 74 68 6F 64 5F 65 78 <?php..method_ex 69 73 74 73 28 27 53 70 6C 46 69 6C 65 4F 62 6A ists('SplFileObj 65 63 74 27 2C 20 27 66 72 65 61 64 27 29 20 6F ect', 'fread') o 72 20 64 69 65 28 22 4E 6F 20 53 70 6C 46 69 6C r die("No SplFil 65 4F 62 6A 65 63 74 3A 3A 66 72 65 61 64 28 29 eObject::fread() 22 29 3B 0A 24 77 69 64 74 68 20 3D 20 31 36 3B ");.$width = 16; 0A 24 68 65 78 5F 77 69 64 74 68 20 3D 20 33 20 .$hex_width = 3 2A 20 24 77 69 64 74 68 3B 0A 24 66 69 6C 65 20 * $width;.$file 3D 20 6E 65 77 20 53 70 6C 46 69 6C 65 4F 62 6A = new SplFileObj 65 63 74 28 5F 5F 46 49 4C 45 5F 5F 29 3B 0A 77 ect(__FILE__);.w 68 69 6C 65 20 28 24 66 69 6C 65 2D 3E 76 61 6C hile ($file->val 69 64 28 29 29 20 7B 0A 09 24 63 68 75 6E 6B 20 id()) {..$chunk 3D 20 24 66 69 6C 65 2D 3E 66 72 65 61 64 28 24 = $file->fread($ 77 69 64 74 68 29 3B 0A 20 20 20 20 24 68 65 78 width);. $hex 20 20 20 3D 20 61 72 72 61 79 5F 6D 61 70 28 66 = array_map(f 75 6E 63 74 69 6F 6E 20 28 24 63 68 61 72 29 20 unction ($char) 7B 20 72 65 74 75 72 6E 20 73 70 72 69 6E 74 66 { return sprintf 28 27 25 30 32 58 27 2C 20 6F 72 64 28 24 63 68 ('%02X', ord($ch 61 72 29 29 3B 20 7D 2C 20 73 74 72 5F 73 70 6C ar)); }, str_spl 69 74 28 24 63 68 75 6E 6B 29 29 3B 0A 20 20 20 it($chunk));. 20 70 72 69 6E 74 66 28 22 25 2D 27 20 7B 24 68 printf("%-' {$h 65 78 5F 77 69 64 74 68 7D 73 20 20 20 20 25 73 ex_width}s %s 5C 6E 22 2C 20 69 6D 70 6C 6F 64 65 28 27 20 27 \n", implode(' ' 2C 20 24 68 65 78 29 2C 20 70 72 65 67 5F 72 65 , $hex), preg_re 70 6C 61 63 65 28 27 2F 5B 5E 5B 3A 70 72 69 6E place('/[^[:prin 74 3A 5D 5D 2F 27 2C 20 27 2E 27 2C 20 24 63 68 t:]]/', '.', $ch 75 6E 6B 29 29 3B 0A 7D unk));.}
Output for 5.3.0 - 5.4.45
No SplFileObject::fread()
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_FUNCTION, expecting ')' in /in/DQgb6 on line 9
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_FUNCTION, expecting ')' in /in/DQgb6 on line 9
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `')'' in /in/DQgb6 on line 9
Process exited with code 255.