/
index.php
79 lines (61 loc) · 1.8 KB
/
index.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
76
77
78
79
<?php
ini_set('display_errors', true);
define('PATH', dirname(__FILE__));
require_once PATH . '/vendor/autoload.php';
use Symfony\Component\DomCrawler\Crawler;
$error = null;
$userSelector = '';
$userHtml = '';
$crawlerHtml = '';
$tidyCrawlerHtml = '';
$resultCount = '';
$resultText = '';
if ($_SERVER['REQUEST_METHOD'] == 'POST') try {
// grab the user's post'd html
$userHtml = isset($_POST['html']) ? $_POST['html'] : null;
if ( ! $userHtml) {
throw new Exception('Please enter some html');
}
// and the user's post'd css selector
$userSelector = isset($_POST['selector']) ? $_POST['selector'] : null;
if ( ! $userSelector) {
throw new Exception('Please enter a CSS selector');
}
$crawler = new Crawler();
$crawler->addHtmlContent($userHtml);
$crawlerHtml = $crawler->html(); // to use later in view
$result = $crawler->filter($userSelector);
$resultCount = count($result);
if ($resultCount > 0) {
$resultText = $result->text();
}
// if no results found yet, then try tidying the html
else {
// tidy version of it
$tidyHtml = tidy_parse_string($userHtml);
$tidyCrawler = new Crawler();
$tidyCrawler->addHtmlContent($tidyHtml);
$tidyCrawlerHtml = $tidyCrawler->html(); // to use later in view
$result = $tidyCrawler->filter($userSelector);
$resultCount = count($result);
if ($resultCount > 0) {
$resultText = $result->text();
}
}
} catch (Exception $e) {
$error = $e->getMessage();
}
// view function to html escape
function escape($string) {
return htmlspecialchars($string, ENT_QUOTES, 'utf-8');
}
$view = (object) [
'error' => $error,
'userSelector' => $userSelector,
'userHtml' => $userHtml,
'crawlerHtml' => $crawlerHtml,
'tidyCrawlerHtml' => $tidyCrawlerHtml,
'resultCount' => $resultCount,
'resultText' => $resultText,
];
include PATH . '/views/index.php';