i Have create php page where use many div with different id name.
so i want to get data or value from one div.
Here am showing one div with id name
i want to get data or value from this div.
<div id="tablename">tablename</div>
i have use this but its not working.
$doc = new DomDocument();
$thediv = $doc->getElementById('tablename');
echo $thediv->textContent;
So please tell me how can i get this value from my div?
You need to pass the whole content of your page to the class, otherwise, it can't select nothing since it thinks the document is empty:
$content = '<div id="tablename"></div>';
$doc = new DomDocument();
$doc->loadHTML($content); // That's the addition
$thediv = $doc->getElementById('tablename');
echo $thediv->textContent;
More info:
loadHTML(): Load the HTML from a string.
loadHTMLFile(): Load the HTML from a file.
Downloaded and include PHP Simple HTML DOM Parser from https://sourceforge.net/projects/simplehtmldom/files/ and
Try this
include 'simple_html_dom.php';
$html = file_get_html("http://www.facebook.com");
$displaybody = $html->find('div[id=blueBarDOMInspector]', 0)->plaintext;
echo $displaybody ;exit;
Related
I need some help with my php. I'm trying to fetch the list of elements from html tag called streams. When I try to fetch the elements from the html tags, it will not fetch it as it will show as a blank.
Here is the code:
<?php
//ini_set('max_execution_time', 1000);
error_reporting(0);
//$errmsg_arr = array();
//$errflag = false;
$baseUrl = file_get_contents('http://www.example.com/streams.php');
$domdoc = new DOMDocument();
$domdoc->strictErrorChecking = false;
$domdoc->recover=true;
#$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementById('streams');
echo $links;
?>
Here is the html source:
<p id='channels'>BBC One South E</p><p id='streams'>http://www.example.com/live/35vlz78As8/191</p>
Can you please help me with how I can be able to fetch the elements from the id of the html tags?
Remove the # at the begin of $domdc so you can see if some error happen and
You should use textContent
$domdoc->loadHTML($baseUrl);
$links = $domdoc->getElementById('streams');
echo $links->textContent;
I know this topic was posted everywhere, but their question is not I want. I want to insert some HTML codes before the page is loaded without touching the original code in the page.
Suppose my header was rendered by a function called render_header():
function render_body() {
return "<body>
<div class='container'>
<div class='a'>A</div>
<div class='b'>B</div>
</div>
</body>";
}
From now, I want to insert HTML codes using PHP without editing the render_body(). I want a function that insert some divs to container'div.
render_body();
<?php *//Insert '<div class="c" inside div container* ?>
Just as an alternative using XPath - this should load in the output from render_body() to an XML (DOMDocument) object and create an XPath object to query your HTML so you can easily work out where you want to insert the new HTML.
This will probably only work if you're using XML well formed HTML though.
//read in the document
$xml = new DOMDocument();
$xml->loadHTML(render_body());
//create an XPath query object
$xpath = new DOMXpath($xml);
//create the HTML nodes you want to insert
// using $xml->createElement() ...
//find the node to which you want to attach the new content
$xmlDivClassA = $xpath->query('//body/div[#class="a"]')->item(0);
$xmlDivClassA->appendChild( /* the HTML nodes you've previously created */ );
//output
echo $xml->saveHTML();
Took a little while as I had to refer to the documentation ... too much JQuery lately it's ruining my ability to manipulate the DOM without looking things up :\
The only thing I can think of is to turn on output buffering and then use the DOMDocument class to read in the entire buffer and then make changes to it. It is worth doing some reading of the documentation (http://www.php.net/manual/en/book.dom.php) provided in the script...
ie.:
<?php
function render_body() {
return "<body>
<div class='container'>
<div class='a'>A</div>
<div class='b'>B</div>
</div>
</body>";
}
$dom = new DOMDocument();
$dom->loadHTML(render_body());
// get body tag
$body = $dom->getElementsByTagName('body')->item(0);
// add a new element at the end of the body
$element = $dom->createElement('div', 'My new element at the end!');
$body->appendChild($element);
echo $dom->saveHTML(); // echo what is in the dom
?>
EDIT:
As per CD001's suggestions, I have tested this code and it works.
So I have two files one being index.php and the other march.html, I'd like to grab the employee's name out of march.html which I set an id like
<h3 id="name">John Doe</h3>
So how would I go about grabbing that name out of march.html so I can place it in my index.php. If you want some more detail, it's like employee of the month so I need to grab a name from 11 other files so I can reference them in the index.php. I've tried using DomDocument in php but it's showing a lot of trouble but here is that code just because
<?php
$dom = new DomDocument();
$dom->validateOnParse = true;
$dom->loadHtml("march.html");
$name = $dom->getElementById("name");
print $name;
?>
Use nodeValue property to get the value:
<?php
$dom = new DOMDocument();
$dom->validateOnParse = true;
$dom->loadHTML(file_get_contents("march.html"));
$name = $dom->getElementById("name")->nodeValue;
print $name;
?>
I would use a jQuery AJAX call to do that with the specialized ajax function "load"
example:
$('#result').load('ajax/test.html #container');
jQuery load API:
http://api.jquery.com/load/
I have a div named "main" in my page. I put the code to convert a html into pdf using php at the end of page. I want to select the content (div named main contains paragraphs, charts, tables etc.).
How ?
Below code will show you how to get DIV tag's content using PHP code.
PHP Code:
<?php
$content="test.html";
$source=new DOMdocument();
$source->loadHTMLFile($content);
$path=new DOMXpath($source);
$dom=$path->query("*/div[#id='test']");
if (!$dom==0) {
foreach ($dom as $dom) {
print "
The Type of the element is: ". $dom->nodeName. "
<b><pre><code>";
$getContent = $dom->childNodes;
foreach ($getContent as $attr) {
print $attr->nodeValue. "</code></pre></b>";
}
}
}
?>
We are getting DIV tag with ID "test", You can replace it with your desired one.
test.html
<div id="test">This is my content</div>
Output:
The Type of the element is: div
This is my content
You should put the php code into a separate file from the html and use something like DOMDocument to get the content from the div.
$dom = new DOMDocument();
$dom->loadHTMLFile('yourfile.html');
...
You cannot directly interact with the HTML DOM via PHP.
What you could do, is using a with an input containing your content. When submitting the form you can access the data via PHP.
But maybe you want to use Javascript for that task?
Nevertheless, a quick'n'dirty PHP example:
<form action="" method="post">
<textarea name="content">hello world</textarea>
</form>
<?php
if (isset($_POST['content'])) {
echo $_POST['content'];
}
?>
I have this code which getting html code of page and than replace all the HREF attributes of the A' tag to redirect it to my site , than my site load the page and again redirect the links and so on...
<?php
libxml_use_internal_errors(true); // hide the parsing errors
$dom = new DOMDocument; // init new DOMDocument
if($_GET){
$dom->loadHtmlFile($_GET['open']); // getting link to redirect to
}else{
$dom->loadHtmlFile('http://www.stackoverflow.com'); // getting default site
}
$dom->loadHtmlFile('http://www.stackoverflow.com'); // load HTML into it
$xpath = new DOMXPath($dom); // create a new XPath
$nodes = $xpath->query('//a[#href]'); // Find all A elements with a href attribute
foreach($nodes as $node) { // Iterate over found elements
$node->setAttribute('href', 'index.php?open=http://www.stackoverflow.com'.$node->getAttribute('href')); // Change href attribute
}
echo $dom->saveXml(); // output cleaned HTML
?>
the code is perfectly running , the only problem is that it won't load CSS files somehow..
you're more than welcome to test this code and see what's the problems!
here is online version: http://browser.breet.co.il
thank you in advance!
Use saveHTML() instead of saveXml()
Using the last one, there's an xml definition at the start of the printed code so it doesn't parse correctly.