Hello. There is a big html file. I bring it to the page by code

$file = file_get_contents('./file.html', FILE_USE_INCLUDE_PATH); 

As a result, the entire HTML code of the file is displayed. But I don’t need all the code, I just need such a block from this code:

 <TBODY class="content"> <TR CLASS="offline" id="" nopoll=""> <TD>тут контент</TD> </TR> <TR CLASS="online" id="" nopoll=""> <TD>тут контент</TD> </TR> <TR CLASS="connect" id="" nopoll=""> <TD>тут контент</TD> </TR> </TBODY> 

How to display on the page only this block?

Initially I wanted to use Simple HTML DOM Parser - but it doesn’t fit, since HTML files sometimes come non-standard, and it gives an error Call to a member function find() on a non-object

  • domDocument and Xpath //TBODY[@class="content"] - splash58
  • @ splash58 can you please an example? - iKey
  • Is it possible to check that before calling find() that you are calling on a non- null value? - teran
  • use the usual string functions to find the desired <tbody> and discard all unnecessary. the fastest way will be. - teran
  • @teran, and if there are several <tbody> ? - entithat

1 answer 1

With regular expression, take all that <tbody> ... </tbody>

 preg_match('/(<tbody.+?<\/tbody>)/mis', $find, $m) var_dump($m); 

https://regex101.com/r/SqgbqP/1

Note: this only works fine if there is only one table in your './file.html' . Otherwise, you can try using preg_match_all