3v4l.org

run code in 300+ PHP versions simultaneously
<?php $scraper = new DOMScraper(); //example couldent think of a site with an example table $scraper->setSite('http://www.farsnews.com/economy/agriculture')->setSource(); //get only tables with id="some_table_id" or any attribute match eg class="somthing" echo '<div>'.$scraper->getInnerHTML('div','class=ctgnewsmainpane').'</div>'; /** * Generic DOM scapper using DOMDocument and cURL */ Class DOMScraper extends DOMDocument{ public $site; private $source; private $dom; function __construct(){ libxml_use_internal_errors(true); $this->preserveWhiteSpace = false; $this->strictErrorChecking = false; } function setSite($site){ $this->site = $site; return $this; } function setSource(){ if(empty($this->site))return 'Error: Missing $this->site, use setSite() first'; $this->source = $this->get_data($this->site); return $this; } function getInnerHTML($tag, $class=null, $nodeValue = false){ if(empty($this->site))return 'Error: Missing $this->source, use setSource() first'; $this->loadHTML($this->source); $tmp = $this->getElementsByTagName($tag); $ret = null; foreach ($tmp as $v){ if($class !== null){ $attr = explode('=',$class); if($v->getAttribute($attr[0])==$attr[1]){ if($nodeValue == true){ $ret .= trim($v->nodeValue); }else{ $ret .= $this->innerHTML($v); } } }else{ if($nodeValue == true){ $ret .= trim($v->nodeValue); }else{ $ret .= $this->innerHTML($v); } } } return $ret; } function innerHTML($dom){ $ret = ""; $nodes = $dom->childNodes; foreach($nodes as $v){ $tmp = new DOMDocument(); $tmp->appendChild($tmp->importNode($v, true)); $ret .= trim($tmp->saveHTML()); } return $ret; } function get_data($url){ if(function_exists('curl_init')){ $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $data = curl_exec($ch); curl_close($ch); return $data; }else{ return file_get_contents($url); } } } ?>
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo for www.farsnews.com failed: System error in /in/ekWpF on line 84 Warning: file_get_contents(http://www.farsnews.com/economy/agriculture): Failed to open stream: php_network_getaddresses: getaddrinfo for www.farsnews.com failed: System error in /in/ekWpF on line 84 Fatal error: Uncaught ValueError: DOMDocument::loadHTML(): Argument #1 ($source) must not be empty in /in/ekWpF:38 Stack trace: #0 /in/ekWpF(38): DOMDocument->loadHTML('') #1 /in/ekWpF(8): DOMScraper->getInnerHTML('div', 'class=ctgnewsma...') #2 {main} thrown in /in/ekWpF on line 38
Process exited with code 255.
Output for 8.0.13 - 8.0.30
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: System error in /in/ekWpF on line 84 Warning: file_get_contents(http://www.farsnews.com/economy/agriculture): Failed to open stream: php_network_getaddresses: getaddrinfo failed: System error in /in/ekWpF on line 84 Fatal error: Uncaught ValueError: DOMDocument::loadHTML(): Argument #1 ($source) must not be empty in /in/ekWpF:38 Stack trace: #0 /in/ekWpF(38): DOMDocument->loadHTML('') #1 /in/ekWpF(8): DOMScraper->getInnerHTML('div', 'class=ctgnewsma...') #2 {main} thrown in /in/ekWpF on line 38
Process exited with code 255.
Output for 8.0.0 - 8.0.12
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /in/ekWpF on line 84 Warning: file_get_contents(http://www.farsnews.com/economy/agriculture): Failed to open stream: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /in/ekWpF on line 84 Fatal error: Uncaught ValueError: DOMDocument::loadHTML(): Argument #1 ($source) must not be empty in /in/ekWpF:38 Stack trace: #0 /in/ekWpF(38): DOMDocument->loadHTML('') #1 /in/ekWpF(8): DOMScraper->getInnerHTML('div', 'class=ctgnewsma...') #2 {main} thrown in /in/ekWpF on line 38
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.26, 7.3.0 - 7.3.13, 7.3.32 - 7.3.33, 7.4.0 - 7.4.1, 7.4.26 - 7.4.33
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: System error in /in/ekWpF on line 84 Warning: file_get_contents(http://www.farsnews.com/economy/agriculture): failed to open stream: php_network_getaddresses: getaddrinfo failed: System error in /in/ekWpF on line 84 Warning: DOMDocument::loadHTML(): Empty string supplied as input in /in/ekWpF on line 38 Warning: DOMDocument::getElementsByTagName(): Couldn't fetch DOMScraper in /in/ekWpF on line 39 Warning: Invalid argument supplied for foreach() in /in/ekWpF on line 41 <div></div>
Output for 7.2.29 - 7.2.33, 7.3.16 - 7.3.31, 7.4.3 - 7.4.25
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /in/ekWpF on line 84 Warning: file_get_contents(http://www.farsnews.com/economy/agriculture): failed to open stream: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /in/ekWpF on line 84 Warning: DOMDocument::loadHTML(): Empty string supplied as input in /in/ekWpF on line 38 Warning: DOMDocument::getElementsByTagName(): Couldn't fetch DOMScraper in /in/ekWpF on line 39 Warning: Invalid argument supplied for foreach() in /in/ekWpF on line 41 <div></div>

preferences:
325.93 ms | 403 KiB | 376 Q