0

I need a part of code html by a file from file_get_contents(url)

I do

$variableee = file_get_contents("http://url.com/path/to/file");
echo $variableee;

Ok now in Variableee I've all the url's code. In this code there is a part that I need. I need a table with class name "table".

Es.

<div>text</div>
<span> text </span>
<table class="table">
<tr><td>Text that I need</td></tr>
</table>

How I can get it? Sorry for bad english.

1 Answers1

2

If you want the data within PHP itself use the built-in DOM parser,

<?php
 $doc = new DOMDocument();
 $doc->loadHTML($variableee);

  $arr = $doc->getElementsByTagName("table"); // DOMNodeList Object
  foreach($arr as $item) { // DOMElement Object
   echo $item->nodeValue;
 }
?>

EDIT: Parse using the class name with DOMXPath

$doc = new DOMDocument();
$doc->loadHTML($variableee);
$classname = 'table';
$a = new DOMXPath($doc);
$spans = $a->query("//*[contains(concat(' ', normalize-space(@class), '   '), ' $classname ')]");
foreach($spans as $item) { // DOMElement Object
 echo $item->nodeValue;
}
Vishnu Nair
  • 2,395
  • 14
  • 16