windy sinaga windy sinaga - 1 year ago 82
HTML Question

simple html dom grabbing h1 header

I just learning about simple_html_dom.php,
I try to get the h1 content with some class.

<h1 class="entry-title">example for the header</h1>

here the raw html file from the website that i want to get the content.

<header class="entry-header">
<div class="entry-meta">
<span class="cat-links"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="category tag">News</a></span>
<h1 class="entry-title">example for the header</h1>
<div class="entry-meta">
<span class="entry-date"><a href="https://xxxxx/2016/08/11/xxxxxxx" rel="bookmark"><time class="entry-date" datetime="2016-08-11T11:54:07+00:00">11 August 2016</time></a></span>
<span class="byline"><span class="author vcard"><a class="url fn n" href="https://xxxxx/2016/08/11/xxxxxxx" rel="author">wndwnrt</a></span></span>
<span class="comments-link"><a href="https://xxxxx/2016/08/11/xxxxxxx">1 Comment</a></span>

here my code to get the h1 class="entry-title" content (example for the header)

require_once __DIR__.'/simple_html_dom.php';
$html = new simple_html_dom();
$header_1 = $html->find('h1[class="entry-title"]')->innertext;
<table border="1">
<th><?php echo $header_1; ?></th>

when i run the code, the result is error:

Trying to get property of non-object

can anyone tell where is the error? and what i should to do?
Thank you very much.

Answer Source

Yes you see that error because you are passing only one argument to the find function.

$header_1 = $html->find('h1[class="entry-title"]')->innertext

now try this:

$header_1 = $html->find('h1[class="entry-title"]',0)->innertext

because you also have to pass the number of the h1 you are trying to get!