1
0

fuckhtml.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. class fuckhtml{
  3. public function __construct($html = null, $isfile = false){
  4. if($html !== null){
  5. $this->load($html, $isfile);
  6. }
  7. }
  8. public function load($html, $isfile = false){
  9. if(is_array($html)){
  10. if(!isset($html["innerHTML"])){
  11. throw new Exception("(load) Supplied array doesn't contain a innerHTML index");
  12. }
  13. $html = $html["innerHTML"];
  14. }
  15. if($isfile){
  16. $handle = fopen($html, "r");
  17. $fetch = fread($handle, filesize($html));
  18. fclose($handle);
  19. $this->html = $fetch;
  20. }else{
  21. $this->html = $html;
  22. }
  23. $this->strlen = strlen($this->html);
  24. }
  25. public function getElementsByTagName(string $tagname){
  26. $out = [];
  27. /*
  28. Scrape start of the tag. Example
  29. <div class="mydiv"> ...
  30. */
  31. if($tagname == "*"){
  32. $tagname = '[^\/<>\s]+';
  33. }else{
  34. $tagname = preg_quote(strtolower($tagname));
  35. }
  36. preg_match_all(
  37. '/<\s*(' . $tagname . ')(\s(?:[^>\'"]*|"[^"]*"|\'[^\']*\')+)?\s*>/i',
  38. /* '/<\s*(' . $tagname . ')(\s[\S\s]*?)?>/i', */
  39. $this->html,
  40. $starting_tags,
  41. PREG_OFFSET_CAPTURE
  42. );
  43. for($i=0; $i<count($starting_tags[0]); $i++){
  44. /*
  45. Parse attributes
  46. */
  47. $attributes = [];
  48. preg_match_all(
  49. '/([^\/\s\\=]+)(?:\s*=\s*("[^"]*"|\'[^\']*\'|[^\s]*))?/',
  50. $starting_tags[2][$i][0],
  51. $regex_attributes
  52. );
  53. for($k=0; $k<count($regex_attributes[0]); $k++){
  54. if(trim($regex_attributes[2][$k]) == ""){
  55. $attributes[$regex_attributes[1][$k]] =
  56. "true";
  57. continue;
  58. }
  59. $attributes[$regex_attributes[1][$k]] =
  60. trim($regex_attributes[2][$k], "'\" \n\r\t\v\x00");
  61. }
  62. $out[] = [
  63. "tagName" => strtolower($starting_tags[1][$i][0]),
  64. "startPos" => $starting_tags[0][$i][1],
  65. "endPos" => 0,
  66. "startTag" => $starting_tags[0][$i][0],
  67. "attributes" => $attributes,
  68. "innerHTML" => null
  69. ];
  70. }
  71. /*
  72. Get innerHTML
  73. */
  74. // get closing tag positions
  75. preg_match_all(
  76. '/<\s*\/\s*(' . $tagname . ')\s*>/i',
  77. $this->html,
  78. $regex_closing_tags,
  79. PREG_OFFSET_CAPTURE
  80. );
  81. // merge opening and closing tags together
  82. for($i=0; $i<count($regex_closing_tags[1]); $i++){
  83. $out[] = [
  84. "tagName" => strtolower($regex_closing_tags[1][$i][0]),
  85. "endTag" => $regex_closing_tags[0][$i][0],
  86. "startPos" => $regex_closing_tags[0][$i][1]
  87. ];
  88. }
  89. usort(
  90. $out,
  91. function($a, $b){
  92. return $a["startPos"] > $b["startPos"];
  93. }
  94. );
  95. // computer the indent level for each element
  96. $level = [];
  97. $count = count($out);
  98. for($i=0; $i<$count; $i++){
  99. if(!isset($level[$out[$i]["tagName"]])){
  100. $level[$out[$i]["tagName"]] = 0;
  101. }
  102. if(isset($out[$i]["startTag"])){
  103. // encountered starting tag
  104. $level[$out[$i]["tagName"]]++;
  105. $out[$i]["level"] = $level[$out[$i]["tagName"]];
  106. }else{
  107. // encountered closing tag
  108. $out[$i]["level"] = $level[$out[$i]["tagName"]];
  109. $level[$out[$i]["tagName"]]--;
  110. }
  111. }
  112. // if the indent level is the same for a div,
  113. // we encountered _THE_ closing tag
  114. for($i=0; $i<$count; $i++){
  115. if(!isset($out[$i]["startTag"])){
  116. continue;
  117. }
  118. for($k=$i; $k<$count; $k++){
  119. if(
  120. isset($out[$k]["endTag"]) &&
  121. $out[$i]["tagName"] == $out[$k]["tagName"] &&
  122. $out[$i]["level"]
  123. === $out[$k]["level"]
  124. ){
  125. $startlen = strlen($out[$i]["startTag"]);
  126. $endlen = strlen($out[$k]["endTag"]);
  127. $out[$i]["endPos"] = $out[$k]["startPos"] + $endlen;
  128. $out[$i]["innerHTML"] =
  129. substr(
  130. $this->html,
  131. $out[$i]["startPos"] + $startlen,
  132. $out[$k]["startPos"] - ($out[$i]["startPos"] + $startlen)
  133. );
  134. $out[$i]["outerHTML"] =
  135. substr(
  136. $this->html,
  137. $out[$i]["startPos"],
  138. $out[$k]["startPos"] - $out[$i]["startPos"] + $endlen
  139. );
  140. break;
  141. }
  142. }
  143. }
  144. // filter out ending divs
  145. for($i=0; $i<$count; $i++){
  146. if(isset($out[$i]["endTag"])){
  147. unset($out[$i]);
  148. }
  149. unset($out[$i]["startTag"]);
  150. }
  151. return array_values($out);
  152. }
  153. public function getElementsByAttributeName(string $name, $collection = null){
  154. if($collection === null){
  155. $collection = $this->getElementsByTagName("*");
  156. }elseif(is_string($collection)){
  157. $collection = $this->getElementsByTagName($collection);
  158. }
  159. $return = [];
  160. foreach($collection as $elem){
  161. foreach($elem["attributes"] as $attrib_name => $attrib_value){
  162. if($attrib_name == $name){
  163. $return[] = $elem;
  164. continue 2;
  165. }
  166. }
  167. }
  168. return $return;
  169. }
  170. public function getElementsByFuzzyAttributeValue(string $name, string $value, $collection = null){
  171. $elems = $this->getElementsByAttributeName($name, $collection);
  172. $value = explode(" ", $value);
  173. $return = [];
  174. foreach($elems as $elem){
  175. foreach($elem["attributes"] as $attrib_name => $attrib_value){
  176. $attrib_value = explode(" ", $attrib_value);
  177. $ac = count($attrib_value);
  178. $nc = count($value);
  179. $cr = 0;
  180. for($i=0; $i<$nc; $i++){
  181. for($k=0; $k<$ac; $k++){
  182. if($value[$i] == $attrib_value[$k]){
  183. $cr++;
  184. }
  185. }
  186. }
  187. if($cr === $nc){
  188. $return[] = $elem;
  189. continue 2;
  190. }
  191. }
  192. }
  193. return $return;
  194. }
  195. public function getElementsByAttributeValue(string $name, string $value, $collection = null){
  196. $elems = $this->getElementsByAttributeName($name, $collection);
  197. $return = [];
  198. foreach($elems as $elem){
  199. foreach($elem["attributes"] as $attrib_name => $attrib_value){
  200. if($attrib_value == $value){
  201. $return[] = $elem;
  202. continue 2;
  203. }
  204. }
  205. }
  206. return $return;
  207. }
  208. public function getElementById(string $idname, $collection = null){
  209. $id = $this->getElementsByAttributeValue("id", $idname, $collection);
  210. if(count($id) !== 0){
  211. return $id[0];
  212. }
  213. return false;
  214. }
  215. public function getElementsByClassName(string $classname, $collection = null){
  216. return $this->getElementsByFuzzyAttributeValue("class", $classname, $collection);
  217. }
  218. public function getTextContent($html, $whitespace = false, $trim = true){
  219. if(is_array($html)){
  220. if(!isset($html["innerHTML"])){
  221. throw new Exception("(getTextContent) Supplied array doesn't contain a innerHTML index");
  222. }
  223. $html = $html["innerHTML"];
  224. }
  225. $html =
  226. preg_split('/\n|<\/?br>/i', $html);
  227. $out = "";
  228. for($i=0; $i<count($html); $i++){
  229. $tmp =
  230. html_entity_decode(
  231. strip_tags(
  232. $html[$i]
  233. ),
  234. ENT_QUOTES | ENT_XML1, "UTF-8"
  235. );
  236. if($trim){
  237. $tmp = trim($tmp);
  238. }
  239. $out .= $tmp;
  240. if($whitespace === true){
  241. $out .= "\n";
  242. }else{
  243. $out .= " ";
  244. }
  245. }
  246. if($trim){
  247. return trim($out);
  248. }
  249. return $out;
  250. }
  251. public function parseJsObject(string $json){
  252. $bracket = false;
  253. $is_close_bracket = false;
  254. $escape = false;
  255. $json_out = null;
  256. $last_char = null;
  257. for($i=0; $i<strlen($json); $i++){
  258. switch($json[$i]){
  259. case "\"":
  260. case "'":
  261. if($escape === true){
  262. break;
  263. }
  264. if($json[$i] == $bracket){
  265. $bracket = false;
  266. $is_close_bracket = true;
  267. }else{
  268. if($bracket === false){
  269. $bracket = $json[$i];
  270. }
  271. }
  272. break;
  273. default:
  274. $is_close_bracket = false;
  275. break;
  276. }
  277. $escape = $json[$i] == "\\" ? true : false;
  278. if(
  279. $bracket === false &&
  280. $is_close_bracket === false
  281. ){
  282. // here we know we're not iterating over a quoted string
  283. switch($json[$i]){
  284. case "[":
  285. case "{":
  286. // dont execute whats in "default"
  287. $json_out .= $json[$i];
  288. break;
  289. case "]":
  290. case "}":
  291. case ",":
  292. case ":":
  293. if(!in_array($last_char, ["[", "{", "}", "]", "\""])){
  294. $json_out .= "\"";
  295. }
  296. $json_out .= $json[$i];
  297. break;
  298. default:
  299. if(in_array($last_char, ["{", "[", ",", ":"])){
  300. $json_out .= "\"";
  301. }
  302. $json_out .= $json[$i];
  303. break;
  304. }
  305. }else{
  306. $json_out .= $json[$i];
  307. }
  308. $last_char = $json[$i];
  309. }
  310. return json_decode($json_out, true);
  311. }
  312. }
  313. ?>