Get td contain table from library simplehtmldom - php

simple_html_dom does not work in page "https://eldni.com/buscar-por-dni?dni=44626399"
<?php
include_once './simple_html_dom./HtmlWeb.php';
use simplehtmldom\HtmlWeb;
// get DOM from URL or file
$doc = new HtmlWeb();
$html = $doc->load('https://eldni.com/buscar-por-dni?dni=44626399');
foreach($html->find('td') as $e)
echo $e->plaintext . '<br>' . PHP_EOL;
?>
I want td plain text of the "td" table.

Related

How to access an HTML attribute and retrieve data from it in PHP?

I'm new to PHP and I would like to know how to retrieve data from an HTML element such as an src?
It's very easy to do that in jQuery:
$('img').attr('src');
But I have no idea how to do it in PHP (if it is possible).
Here's an example I'm working on:
I loaded $result into SimpleXMLElement and stored it into $xml:
$xml = simplexml_load_string($result) or die("Error: Cannot create object");
Then used foreach to loop over all elements:
foreach($xml->links->link as $link){
echo 'Image: ' . $link->{'link-code-html'}[0] . '</br>';
// returns sometihing similar to: <a href='....'><img src='....'></a>
}
Inside of the foreach I'm trying to access links (src) in img.
Is there a way to access src of the img nested inside of the a — clear when outputted to the screen:
echo 'Image: ' . $link->{'link-code-html'}[0] . '</br>';
I would do this with the built-in DOMDocument and DOMXPath APIs, and then you can use the getAttribute method on any matching img node:
$doc = new DOMDocument();
// Load some example HTML. If you need to load from file, use ->loadHTMLFile
$doc->loadHTML("<a href='abc.com'><img src='ping1.png'></a>
<a href='def.com'><img src='ping2.png'></a>
<a href='ghi.com'>something else</a>");
$xpath = new DOMXpath($doc);
// Collect the images that are children of anchor elements
$imgs = $xpath->query("//a/img");
foreach($imgs as $img) {
echo "Image: " . $img->getAttribute("src") . "\n";
}

Retrieve the DOM from a variable with Simple HTML DOM Parser?

I'm using Simple HTML DOM Parser to retrieve informations from a website with this code:
$html = file_get_html("http://www.example.com/"]);
$table = $html->find("div[class=table]");
foreach ( $table as $tabella ) {
$title = $tabella->find (".elementTitle");
echo "<h2>" . $title[0] -> plaintext . "</h2>";
$minisito = $tabella->find ("h1[class=elementTitle] a");
echo "<p>" . $minisito[0] -> href . "</p>";
}
Now I need to extract other pieces of contents from the url contained in this specific urls $minisito[0] -> href
How can I create another variable using file_get_html command to extract data from this new urls?

PHPHtmlParser getAttribute not works for custom attributes

I have some HTML with custom attributes and trying to parse it with component PHPHtmlParser. Whole project created via this component. Here is the problem example given.
use PHPHtmlParser\Dom;
class Parsemydiv {
function parseAttr()
{
$str='<div otop="20" oleft="20" name="info">
<img src="example.jpg">
</div>';
$dom = new Dom();
$dom->loadStr($str);
$otop = $dom->getAttribute("otop");
$name = $dom->getAttribute("name");
echo "Name: " . $name . PHP_EOL;
echo "Top: " . $otop . PHP_EOL;
echo "Left: " . $oleft . PHP_EOL;
}
}
Output is:
Name: info
Top:
Left:
getAttribute cannot get custom attributes.
Why use a 3rd party library to parse the DOM when PHP has built-in support for this? I suggest learning the native functions instead:
$str='<div otop="20" oleft="15" name="info">
<img src="example.jpg">
</div>';
$doc = new DOMDocument();
$doc->loadHTML($str);
$div = $doc->getElementsByTagName('div')[0];
$otop = $div->getAttribute('otop');
$oleft = $div->getAttribute('oleft');
echo "otop=$otop, oleft=$oleft"; //otop=20, oleft=15

All divs are not coming while parsing html by dom file

I am trying to parse all divs by using DOM file but all the divs are not coming.
My code is:
<?php
include('simplehtmldom/simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.ebay.in');
foreach($html->find('div') as $element)
echo $element->class . '<br>';
?>

PHP way of parsing HTML string

I have a php string that contains the below HTML I am retrieving from an RSS feed. I am using simple pie and cant find any other way of splitting these two datasets it gets from <description>. If anyone knows of a way in simple pie to select children that would be great.
<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>
to:
$image = '<img title="example" alt="example" src="example.jpg">';
$description = 'EXAMPLE TEXT';
$received_str = 'Your received html';
$html = str_get_html($received_str);
//Image tag
$img_tag = $html->find("img", 0)->outertext;
//Example Text
$example_text = $html->find('div[style=example]', 0)->last_child()->innertext;
See Here: http://simplehtmldom.sourceforge.net/manual.htm
Try Simple HTML Dom Parser
// Create DOM from HTML string
$html = str_get_html('Your HTML here');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Description
$description = $html->find('div[style=example]');
try using strip_tags:
<?php
$html ='<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>';
$html = strip_tags($html,'<img>');
// $html == '<img title="example" alt="example" src="example.jpg">'
?>

Categories