forked from salsify/jsonstreamingparser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_regex.php
More file actions
78 lines (66 loc) · 2.57 KB
/
example_regex.php
File metadata and controls
78 lines (66 loc) · 2.57 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<?php
declare(strict_types=1);
use JsonStreamingParser\Listener\RegexListener;
use JsonStreamingParser\Parser;
require_once __DIR__.'/../vendor/autoload.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);
$filename = __DIR__.'/../tests/data/example.json';
echo "Check where the 'name' elements are ('(.*/name)')...".PHP_EOL;
$listener = new RegexListener(['(.*/name)' => function ($data, $path): void {
echo 'Location is '.$path.' value is '.$data.PHP_EOL;
}]);
$fp = fopen($filename, 'r');
$parser = new Parser($fp, $listener);
$parser->parse();
fclose($fp);
echo PHP_EOL."Extract the second 'name' element ('/1/name')...".PHP_EOL;
$listener = new RegexListener(['/1/name' => function ($data): void {
echo "Value for '/1/name' is ".$data.PHP_EOL;
}]);
$fp = fopen($filename, 'r');
$parser = new Parser($fp, $listener);
$parser->parse();
fclose($fp);
echo PHP_EOL."Extract each base element ('(/\d*)') and print 'name' element of this...".PHP_EOL;
$listener = new RegexListener(['(/\d*)' => function ($data, $path): void {
echo 'Location is '.$path.' value is '.$data['name'].PHP_EOL;
}]);
$fp = fopen($filename, 'r');
$parser = new Parser($fp, $listener);
$parser->parse();
fclose($fp);
echo PHP_EOL."Extract 'nested array' element ('(/.*/nested array)')...".PHP_EOL;
$listener = new RegexListener(['(/.*/nested array)' => function ($data, $path): void {
echo 'Location is '.$path.' value is '.print_r($data, true).PHP_EOL;
}]);
$fp = fopen($filename, 'r');
$parser = new Parser($fp, $listener);
$parser->parse();
fclose($fp);
echo PHP_EOL.PHP_EOL.'Combine above...'.PHP_EOL;
$listener = new RegexListener([
'/1/name' => function ($data): void {
echo PHP_EOL."Extract the second 'name' element...".PHP_EOL;
echo '/1/name='.print_r($data, true).PHP_EOL;
},
'(/\d*)' => function ($data, $path): void {
echo PHP_EOL."Extract each base element and print 'name'...".PHP_EOL;
echo $path.'='.$data['name'].PHP_EOL;
},
'(/.*/nested array)' => function ($data, $path): void {
echo PHP_EOL."Extract 'nested array' element...".PHP_EOL;
echo $path.'='.print_r($data, true).PHP_EOL;
},
]);
$parser = new Parser(fopen($filename, 'r'), $listener);
$parser->parse();
$filename = __DIR__.'/../tests/data/ratherBig.json';
echo 'With a large file extract totals from header and stop...'.PHP_EOL;
$listener = new RegexListener();
$parser = new Parser(fopen($filename, 'r'), $listener);
$listener->setMatch(['/total_rows' => function ($data) use ($parser): void {
echo '/total_rows='.$data.PHP_EOL;
$parser->stop();
}]);
$parser->parse();