|
| 1 | +<?php |
| 2 | + |
| 3 | +require_once 'simple_html_dom.php'; |
| 4 | + |
| 5 | +class GrabberText { |
| 6 | + |
| 7 | + private $urlPattern = array(); |
| 8 | + private $fillHorizontal = false; |
| 9 | + |
| 10 | + private function horizontal($arr) { |
| 11 | + $arrH = array(); |
| 12 | + |
| 13 | + foreach ($arr as $cur) { |
| 14 | + |
| 15 | + foreach ($cur as $block) { |
| 16 | + if ($arr["h1"][0] != $block) |
| 17 | + @$arrH[$arr["h1"][0]][] = $block; |
| 18 | + } |
| 19 | + } |
| 20 | + |
| 21 | + return $arrH; |
| 22 | + } |
| 23 | + |
| 24 | + public function setFilHorizontal($bool) { |
| 25 | + $this->fillHorizontal = $bool; |
| 26 | + } |
| 27 | + |
| 28 | + public function addUrlPattern($urlPattern) { |
| 29 | + |
| 30 | + $this->urlPattern[] = $urlPattern; |
| 31 | + } |
| 32 | + |
| 33 | + public function getUrlPatterns() { |
| 34 | + return $this->urlPattern; |
| 35 | + } |
| 36 | + |
| 37 | + public function getTexts($url) { |
| 38 | + |
| 39 | + $texts = array(); |
| 40 | + |
| 41 | + $html = file_get_html($url); |
| 42 | + |
| 43 | + if (is_object($html)) { |
| 44 | + |
| 45 | + foreach ($this->getUrlPatterns() as $pattern) { |
| 46 | + foreach ($html->find($pattern) as $text) { |
| 47 | + if(isset($text->src)) |
| 48 | + $texts[$pattern." src"][] = $text->src; |
| 49 | + |
| 50 | + $text = strip_tags($text); |
| 51 | + |
| 52 | + $text = str_replace("|", "", $text); |
| 53 | + $text = str_replace("\n", "", $text); |
| 54 | + $text = str_replace("\r", "", $text); |
| 55 | + $text = str_replace("'", "", $text); |
| 56 | + $text = str_replace("\"", "", $text); |
| 57 | + $text = str_replace(" ", "", $text); |
| 58 | + if (strlen($text) > 5) |
| 59 | + $texts[$pattern][] = $text; |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + if ($this->fillHorizontal) { |
| 65 | + return $this->horizontal($texts); |
| 66 | + } |
| 67 | + return $texts; |
| 68 | + } |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +/* |
| 73 | +Use |
| 74 | +$gl = new GrabberText(); |
| 75 | +$gl->addUrlPattern('.authors'); |
| 76 | +$gl->addUrlPattern('.quote'); |
| 77 | +
|
| 78 | +print_r($gl->getTexts("https://www.quotetab.com/quotes/by-richard-bach")); |
| 79 | + * |
| 80 | + */ |
| 81 | + |
0 commit comments