-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.php
43 lines (29 loc) · 888 Bytes
/
parse.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
<?php
// requires DOM extension: sudo apt-get install php-dom
$file = './publications.html';
// https://stackoverflow.com/a/2087136/426266
function DOMinnerHTML(DOMNode $element)
{
$innerHTML = "";
$children = $element->childNodes;
foreach ($children as $child)
{
$innerHTML .= $element->ownerDocument->saveHTML($child);
}
return trim($innerHTML);
}
$html = file_get_contents($file);
$classname = "bibtexitem";
$doc = new DOMDocument();
$doc->loadHTML($html);
$finder = new DomXPath($doc);
$rows = $finder->query("//*[contains(@class, '$classname')]");
// $rows = $doc->getElementsByTagName('td');
$references = [];
foreach ($rows as $bibitem) {
// var_dump(DOMinnerHTML($bibitem));
$references[] = DOMinnerHTML($bibitem);
}
$fp = fopen('references.json', 'w');
fwrite($fp, json_encode($references, JSON_PRETTY_PRINT));
fclose($fp);