-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonPrintr.php
executable file
·59 lines (45 loc) · 1.37 KB
/
jsonPrintr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/env php
<?php $header = <<<COMMENT
JSON print_r v201504172014
Louis T. Getterman IV (@LTGIV)
www.GotGetLLC.com | www.opensour.cc/programming/php
Thanks:
http://stackoverflow.com/questions/11968244/php-reading-line-by-line-from-stdin
http://stackoverflow.com/questions/3684367/php-cli-how-to-read-a-single-character-of-input-from-the-tty-without-waiting-f
COMMENT;
$header = implode( "\n", array_slice( explode( "\n", trim($header) ), 0, 3) );
// Loop
while(1) {
system('clear');
echo "{$header}\n";
echo str_repeat( '-', 79 )."\n";
echo "Paste JSON contents in, press CTRL-D (^D) on a newline when finished.\n";
echo str_repeat( '-', 79 )."\n";
$json = '';
while( $f = fgets(STDIN) ){
$json .= $f;
} // END WHILE LOOP
$json = trim($json);
system('clear');
echo "{$header}\n";
echo str_repeat( '-', 79 )."\n";
echo "Output in PHP's print_r() format:\n";
echo str_repeat( '-', 79 )."\n";
print_r( json_decode($json) );
echo str_repeat( '-', 79 )."\n";
// Save existing tty configuration
$term = `stty -g`;
// Make changes to the tty
system("stty -icanon");
// Prompt
echo "Parse more JSON? : ";
$loopCont = fread(STDIN, 1);
// Reset the tty back to the original configuration
system("stty '" . $term . "'");
if ( strtoupper($loopCont) == 'N' ) {
echo "\n";
echo str_repeat( '-', 79 )."\n";
echo "\n";
break;
} // END IF
} // END WHILE LOOP