I've searched around for solutions to this question, but each one i find, and try doesn't work.
I'm trying to grab the content of a div from a forum topic.
I've tried using preg_match and that only displayed "Array" then I tried using this method
$html = file_get_contents("http://www.lcs-server.co.uk/forum/index.php/topic,$id_topic");
$dom = new DOMDocument;
$dom->loadHTML($html);
$element = $dom->getElementById("msg_$id_msg");
var_dump($element);
This will show "object(DOMElement)#1 (0) { } "
The $id_topic and $id_msg are defined above this code, taken from the forum database. I did try taking the message from the forum database, but it displayed BB code tags, I'd like it to grab the post content, and display it in HTML, as it's displayed on the forum post itself.
This is the code I'm using now and giving me "Fatal error: Cannot redeclare DOMinnerHTML()"
$html = file_get_contents("http://www.lcs-server.co.uk/forum/index.php/topic,$id_topic");
$dom = new DOMDocument;
$dom->loadHTML($html);
$domelement = $dom->getElementById("msg_$id_msg");
foreach ($domelement as $element)
{
echo DOMinnerHTML($element);
}
function DOMinnerHTML($DOMelement)
{
$innerHTML = "";
$children = $DOMelement->childNodes;
foreach ($children as $child)
{
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($child, true));
$innerHTML.=trim($tmp_dom->saveHTML());
}
return $innerHTML;
}
getElementById returns a DOM node object. It does not return the HTML of the node. For that, you have to get the node's "innerHTML". This properly is not officially supported by PHP's dom object for some reason, but can be faked using this answer: How to get innerHTML of DOMNode?
Related
I am working with php and I am trying to get certain data from a webpage
everything works till i get to this part:
<a class="cleanthis" href="https://www.web.com" id="1122" rel="#1122" style="display: inline-block;"><strong>the data i want</strong></a>
As you can see i want the data in strong but i cant get it. I only get blank lines
code i use:
foreach($as as $a) {
if ($a->getAttribute('class') === 'cleanthis') {
$strong = $a->getElementsByTagName('strong');
echo $strong->nodeValue;;
}
You should be seeing this error message:
Undefined property: DOMNodeList::$nodeValue
That is because $strong = $a->getElementsByTagName('strong'); will put a DOMNodeList in $string. You either need to iterate the list or retrieve the actual node from it, e.g.
echo $strong->item(0)->nodeValue;
Or you can just use XPath:
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
foreach ($xpath->evaluate('//a[#class="cleanthis"]/strong/text()') as $element) {
echo $element->nodeValue, PHP_EOL;
}
I get this error message from PHP: "undefined method DOMNodeList::setAttribute()" from line 9. I am trying to change the src of an image in my HTML at my server and so far this is my code:
<?php
if (isset($_POST['id']) && isset($_POST['name']))
{
$id = $_POST['id'];
$name = $_POST['name'];
$html = $_POST['html'];
$dom = new domDocument;
$dom->loadHTML($html);
$node = $dom->getElementsByTagName( 'img' );
$node ->setAttribute('src', 'images/' . $name);//line 9
echo $dom->saveHTML();
}
echo 'error';
exit;
//html
<div><img id="picture" src=""></div>
The variable 'id' is the HTML id of the specific line of HTML, name is the the name of the image and HTML is the line of HTML.
As far as I can understand from researching I select a specific line of HTML which I then load to my DOM variable. I then specify the element ie: "img" which I can then edit through the use of setAttribute however this does not work. I only want to change the source of this one img with the ID of "picture".
But the DOMNodeList doesn't have that method.
getElementsByTagName is part of the DOMDocument class.
You don't need to cast anything, just call the method:
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
$spans = $link->getElementsByTagName('span');
}
And by the way, DOMElement is a subclass of DOMNode. If you were talking about a DOMNodeList, then accessing the elements in such a list can be done, be either the method presented above, with a foreach loop, either by using the item() method of DOMNodeList
$link_0 = $dom->getElementsByTagName('a')->item(0);
getElementsByTagName returns a list of nodes. Just try with:
$nodes = $dom->getElementsByTagName('img');
foreach ($nodes as $node) {
$node->setAttribute('src', 'images/' . $name);
}
I know there are similar question, but, trying to study PHP I met this error and I want understand why this occurs.
<?php
$url = 'http://aice.anie.it/quotazione-lme-rame/';
echo "hello!\r\n";
$html = new DOMDocument();
#$html->loadHTML($url);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query(".//*[#id='table33']/tbody/tr[2]/td[3]/b");
foreach ($nodelist as $n) {
echo $n->nodeValue . "\n";
}
?>
this prints just "hello!". I want to print the value extracted with the xpath, but the last echo doesn't do anything.
You have some errors in your code :
You try to get the table from the url http://aice.anie.it/quotazione-lme-rame/, but it's actually in an iframe located at http://www.aiceweb.it/it/frame_rame.asp, so get the iframe url directly.
You use the function loadHTML(), which load an HTML string. What you need is the loadHTMLFile function, which takes the link of an HTML document as a parameter (See http://www.php.net/manual/fr/domdocument.loadhtmlfile.php)
You assume there is a tbody element on the page but there is no one. So remove that from your query filter.
Working code :
$url = 'http://www.aiceweb.it/it/frame_rame.asp';
echo "hello!\r\n";
$html = new DOMDocument();
#$html->loadHTMLFile($url);
$xpath = new DOMXPath($html);
$nodelist = $xpath->query(".//*[#id='table33']/tr[2]/td[3]/b");
foreach ($nodelist as $n) {
echo $n->nodeValue . "\n";
}
I'm having trouble trying to write an if statement for the DOM that will check if $html is blank. However, whenever the HTML page does end up blank, it just removes everything that would be below DOM (including what I had to check if it was blank).
$html = file_get_contents("http://example.com/");
$dom = new DOMDocument;
#$dom->loadHTML($html);
$links = $dom->getElementById('dividhere')->getElementsByTagName('img');
foreach ($links as $link)
{
echo $link->getAttribute('src');
}
All this does is grab an image URL in the specified div, which works perfectly until the page is a blank HTML page.
I've tried using SimpleHTMLDOM, which didn't work either (it didn't even fetch the image on working pages). Did I happen to miss something with this one or am I just missing something in both?
include_once('simple_html_dom.php')
$html = file_get_html("http://example.com/");
foreach($html->find('div[id="dividhere"]') as $div)
{
if(empty($div->src))
{
continue;
}
echo $div->src;
}
Get rid on the $html variable and just load the file into $dom by doing #$dom->loadHTMLFile("http://example.com/");, then have an if statement below that to check if $dom is empty.
I get a page using file_get_contents from a remote server, but I want to filter that page and get a DIV from it that has class "text" using PHP. I started with DOMDocument but I'm lost now.
Any help?
$file = file_get_contents("xx");
$elements = new DOMDocument();
$elements->loadHTML($file);
foreach ($elements as $element) {
if( !is_null($element->attributes)) {
foreach ($element->attributes as $attrName => $attrNode) {
if( $attrName == "class" && $attrNode== "text") {
echo $element;
}
}
}
}
Once you have loaded the document to a DOMDocument instance, you can use XPath queries on it -- which might be easier than going yourself through the DOM.
For that, you can use the DOMXpath class.
For example, you should be able to do something like this :
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$tags = $xpath->query('//div[#class="text"]');
foreach ($tags as $tag) {
var_dump($tag->textContent);
}
(Not tested, so you might need to adapt the XPath query a bit...)
Personally, I like Simple HTML Dom Parser.
include "lib.simple_html_dom.php"
$html = file_get_html('http://scrapeyoursite.com');
$html->find('div.text')->plaintext;
Pretty simple, huh? It accommodates selectors like jQuery :)
you can use simple_html_dom like here simple_html_dom doc
or use my code like here :
include "simple_html_dom.php";
$html = new simple_html_dom();
$html->load_file('www.yoursite.com');
$con_div = $html->find('div',0);//get value plaintext each html
echo the $con_div in plaintext..
$con_div->plaintext;
it's mean you will find the first div in array ('div',0) and show it in plaintext..
i hope it help you :cheer