-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun.php
More file actions
45 lines (33 loc) · 1.03 KB
/
run.php
File metadata and controls
45 lines (33 loc) · 1.03 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
<?php
require_once __DIR__ . "/Matcher.php";
use Atrox\Matcher;
$opts = getopt('f:p:s', ['xml', 'html', 'json', 'dump', 'script:']);
if (!$opts || !isset($opts['f']) || (!isset($opts['p']) && !isset($opts['script']))) {
echo "usage: ".basename(__FILE__)." -f file.html -p xpathPattern --html\n";
echo "usage: ".basename(__FILE__)." -f file.html --script m.php --html\n";
die;
}
if (isset($opts['script'])) {
$m = require_once $opts['script'];
} else {
$mode = (isset($opts['s'])) ? 'single' : 'multi';
$patterns = is_array($opts['p']) ? $opts['p'] : [$opts['p']];
$m = call_user_func_array("Atrox\Matcher::$mode", $patterns);
$m = (isset($opts['html']) || !isset($opts['xml'])) ? $m->fromHtml() : $m->fromXml();
}
$f = file_get_contents($opts['f']);
$res = $m($f);
if (isset($opts['json'])) {
echo json_encode($res, JSON_PRETTY_PRINT), "\n";
} else if (isset($opts['dump'])) {
var_dump($res);
echo "\n";
} else {
if (is_scalar($res)) {
echo $res, "\n";
} else {
foreach ($res as $l) {
echo $l, "\n";
}
}
}