-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.php
75 lines (61 loc) · 3.48 KB
/
parser.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*------------------------------------------------------------------------------
** File: parser.php
** Description: PubMed Link Parser
** Version: 1.0.1
** Updated: November 23rd, 2016
** Author: Christi Richards
** Homepage: https://github.com/christirichards
** Project: https://github.com/christirichards/pubmed-link-parser
**------------------------------------------------------------------------------
** Created for NutritionFacts.org https://github.com/nutritionfactsorg
**------------------------------------------------------------------------------ */
include_once 'inc/simple_html_dom.php';
function getTitle($url, $tagname)
{
$values = array();
$html = file_get_html($url);
foreach ($html->find($tagname) as $tag) {
$values[] = trim($tag->innertext);
}
return $values;
}
$error = '';
if ($_POST) {
$urls = $_POST['urls'];
if (!empty($_POST['urls'])) {
$urls = explode(PHP_EOL, $_POST['urls']);
$urls = array_map('trim', $urls);
$urls = array_filter($urls);
$batch_count = ['First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth'];
$batch_number = 1;
echo '<div class="center-block text-center"><div class="btn-group" role="group" aria-label="Batch Options">
<button type="button" class="btn btn-sm btn-secondary" id="titles"><span class="octicon octicon-info"></span> Titles</button>
<button type="button" class="btn btn-sm btn-secondary" id="numbering"><span class="octicon octicon-list-ordered"></span> Numbering</button>
<button type="button" class="btn btn-sm btn-secondary" id="links"><span class="octicon octicon-link"></span> Links</button>
</div></div>';
foreach (array_chunk($urls, 30, true) as $batch_number => $batch) {
echo '<div class="col-md-4 single-batch">';
echo '<h3>'.$batch_count[$batch_number].' Batch
<button type="button" class="btn btn-sm btn-secondary pull-right" data-clipboard-target="#'.$batch_count[$batch_number].'"><span class="octicon octicon-clippy"></span></button></h3><div id="'.$batch_count[$batch_number].'">';
$url_count = 1;
foreach ($batch as $url) {
$url = trim($url);
if (filter_var($url, FILTER_VALIDATE_URL)) {
$title = getTitle($url, 'h1');
if ($title === false) {
continue;
}
echo '<p><span class="numbering">'.$url_count.'.</span> <a href="'.$url.'" class="link">'.$url.'</a><br />
<span class="title">'.$title[1].'</span></p>';
++$url_count;
} else {
echo '<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><strong>Link Skipped: Not a URL</strong> Please check your input and try again</div>';
}
}
echo '</div></div>';
}
} else {
echo '<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><strong>Warning:</strong> You must enter at least one URL.</div>';
}
}