1
0

fuckhtml.php 8.6 KB

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