Use Dom to find value of script variable - php

Assume a DOM document that has been loaded with an HTML page source. Within that page source is a variable defined inside a <script>:
<!-- a bunch of html page source code -->
<script>
// ... a bunch of code
var foo = "Looking for Mr. Goodbar";
// ... more code
</script>
<!-- the rest of the page code -->
My code has created a new DOM object, and assigned the $html code from a page to that object
$dom = new DOMDocument();
// load html
#$dom->loadHTML($html);
How do I search the DOM object for the value of the 'foo' variable? I want this result:
Looking for Mr. Goodbar
This is what I tried
$foo_value = $xp->query("foo");
Thanks!

Related

How can you hide html element with php?

if you do in php:
$dom = new DOMDocument();
$dom->loadHTML($pageUrl);
$dom->getElementById(eleId);
How can you hide the element. I know how you can do it with css and js. But I want to know it with php.
I’m going to assume that you’re trying to have the element initially hidden i.e. in existence, but not visible, upon the browser’s initial rendering and you already know that PHP can’t directly instruct the browser to hide elements.
Following your example, you could do:
$eleId = “div_to_hide”;
$dom = new DOMDocument();
$dom->loadHTML($pageUrl);
$target_elem = $dom->getElementById($eleId);
if ($target_elem) {
$target_elem-> setAttribute('style','display: none;');
}
Or you could create a “script” element containing JavaScript code to execute after the resource has totally downloaded to hide it at run-time.
Or you could create a “style” element in your DOMDocument object in which you’d put CSS content referencing the target element (by selector) to apply initial “display: none;” CSS.
Just other ideas, but the snippet above follows your paradigm.

Load html content from ajax response

I am creating an application in opencart in which the controller returns output in json form. If i do $('.my-div').html(json['output']) then its working fine. But how can i filter particular div or table from that response and put only that filtered html in my div. I know about $('.my-div').load('index.php?route=mymodule/subpage' .my-div > * ); But it doesn't work as my controller is created to return output not to generate.
What my controller returns is
$json['output'] = $this->load->view('default/template/mymodule/mytemplate.tpl', $data);
Edit
What i am getting in response is
json['output'] = '<div>......</div><table class="myclass"></table>';
I want to load only myclass in my div. So how can i filter only myclass table from response.? I've tried filter but its giving me error.
Like Raghubendra said, it's better to only keep the html you need in the .tpl file, but if you can't do that for whatever reason, you can try to filter it on the server side using xPath [it's not the fastest way, but it's a clean way to get the result]. Here is how I think you can do it:
$output = $this->load->view('default/template/mymodule/mytemplate.tpl', $data);
$dom = new DOMDocument();
#$dom->loadHTML($output); // # is to suppress all warnings related to the fact that html is not a proper dom document
$xpath = new DOMXPath($dom);
$match = $xpath->query('//table[#class="myclass"]');
/* $match is not an array, it implements 'Traversable', you can't access your <table> node using $match[0],
you need a foreach that will only loop once if you have one <table class="myclass"> element */
foreach($match as $table) {
$json['output'] = $table->ownerDocument->saveHTML($table); // saveHTML allows you to get the output in an HTML string
}
N.B: Please note that you need to change this code in case you have multiple <table class="myclass">, I would recommend using iterator_to_array($match); to transform the node list to an array so you can access it using $match[0]... and avoid the foreach.
If you want only a part of the html in your div then why are you rendering the whole tpl file. Why don't you remove the html code that you don't want to use.
Suppose if your mytemplate.tpl has
<div>
---------
--------
</div>
<table class="myclass"></table>
<div>
---------
--------
</div>
Then why don't you only keep the table code are remove all the other code as you are not using it while rendering the tpl file.

PHP DOMDocument is not working

I am studying parsing HTML on PHP and I am using DOM for this.
I write this code inside my php file:
<?php
$site = new DOMDocument();
$div = $site->createElement("div");
$class = $site->createAttribute("class");
$class->nodeValue = "wrapper";
$div->appendChild($class);
$site->appendChild($div);
$html = $site->saveHTML();
echo $html;
?>
And when I run this on the browser and view the page source, only this code comes out:
<div class="wrapper"></div>
I don't know why it is not showing the whole html document that supposedly have to be. I am using XAMPP v3.2.1.
Please tell me where did I gone wrong with this. Thanks.
It's showing the whole HTML you created. A div node with a wrapper class attribute.
See the example in the docs. There the html, head, etc. nodes are explicitly created.
PHP only adds missing DOCTYPE, html and body elements when loading HTML, not when saving.
Adding $site->loadHTML($site->saveHTML()); before $html = $site->saveHTML(); will demonstrate this.

Parsing HTML that has javascript with PHP

I'm trying to parse a HTML page where the majority of the content is contained in javascript. When I use the Chrome development tools I can see that the div class I'm trying to grab the content from is called div class=doodle-image. However when I either view the page as a source or try to grab it with php:
<?php
include_once('simple_html_dom.php');
$html = new simple_html_dom();
$html->load_file('http://www.google.com/doodles/finder/2012/All%20doodles');
$doodles = $html->find('.doodle-image');
echo $html;
?>
It returns the frame of the page but contains none of the divs or content. How can I grab the full content of the page?
That's because the element is empty when your PHP client fetches it, Google is loading in a JSON-object with JavaScript to populate the list of doodles. It does a Ajax-request to this page, and probably you can too.

PHP search content for ID and add Class

I need a simple function that will search my wordpress content for a specific ID, and than add a class to the same element the ID is in.
Its for a video player plugin that displays itself via shortcode. My problem is the plugin gives each element an ID as follows, id="video-1-player", id="video-2-player". So the function needs to search the content for id="video-(any number)-player" and than insert a class in there.
thanks!
EDIT
heres the answer that worked for me.
https://stackoverflow.com/a/6180884/278629
Use the DOMDocument class to represent your document as an object. Query for the ID you're seeking, and add a class onto it. From there you can spit the HTML back out.
Simple example:
// HTML to be handled (could very well be read in)
$html = "<!DOCTYPE html><html><body><p id='foo'>Foo</p></body></html>";
// Create and load our DOMDocument object
$doc = new DOMDocument();
$doc->loadHTML($html);
// Find and manipulate our paragraph
$foo = $doc->getElementById("foo");
$foo->setAttribute("class", "bar");
// Return the entire document HTML
echo $doc->saveHTML();
Alternatively, if you only wanted the HTML for the affected element:
echo $doc->saveHTML($foo);
The generated HTML follows:
<!DOCTYPE html>
<html>
<body>
<p id="foo" class="bar">Foo</p>
</body>
</html>
Note that the above code doesn't first check to see if the class attribute is already present on the element. You should perform that check so as to not lose any pre-existing classes that might already be on the element.

Categories