html - php simple_html_dom select correct table data
Get the solution ↓↓↓Solution:
According to provided html structure you need to change this line:
if($marker->innertext == $needle) {
$table = $marker
// `next_sibling` gets `div class="subtitleBottomEdge"`
->next_sibling()
// `next_sibling` gets `<div class="pad10">`
->next_sibling()
// `first_child` gives you a required table
->first_child();
break;
}
Update for one cell, for example:
foreach($table->children() as $k => $tr) {
$data[$k][] = $tr
// `first_child` gets first `td`
->first_child()
// `next_sibling` gets second `td`
->next_sibling()
->innertext;
}
Answer
Solution:
Step 1. Preparation
The first thing you'll need to do is download a copy of the simpleHTMLdom library, freely available from sourceforge.
There are several files in the download, but the only one you need is the simple_html_dom.php file; the rest are examples and documentation. https://sourceforge.net/projects/simplehtmldom/
Step 2. Parsing Basics
This library is very easy to use, but there are some basics you should review before putting it into action.
Loading HTML
$html = new simple_html_dom();
// Load from a string
$html->load('<html><body><p>Hello World!</p><p>We're here</p></body>
</html>');
// Load a file
$html->load_file('http://net.tutsplus.com/');
You can create your initial object either by loading HTML from a string, or from a file. Loading a file can be done either via URL, or via your local file system. Once you have your DOM object, you can start to work with it by using find() and creating collections. A collection is a group of objects found via a selector - the syntax is quite similar to jQuery. in your own Problem page1.html
<h3 class="subTitle">Odbitki</h3><div class="subtitleBottomEdge"></div>
<div class="pad10"><table class="mainContentArea">
<tr>
<td class="labelFont">Wszystkie odbitki:</td>
<td class="itemFont">49946</td>
</tr>
<tr>
<td class="labelFont">Kompletne odbitki równoważności (A4/Letter):</td>
<td class="itemFont">49945.4</td>
</tr>
<tr>
<td class="labelFont">Arkusze dwustronne:</td>
<td class="itemFont">2735</td>
</tr>
In this example HTML, we're going to take a look at how to access the information in the second paragraph, change it, and then output the results.
# create and load the HTML
include('simple_html_dom.php');
$html = new simple_html_dom();
$html->load("page1.html");
# get an element representing the second paragraph
$element1 = $html->find('.labelFont');
$element2 = $html->find('.itemFont');
# modify it
$element1->innertext .= $element2->innertext;
# output it!
echo $html->save();
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: call to undefined function str_contains()
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.