PHP search content for ID and add Class - php

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.

Related

Use Dom to find value of script variable

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!

how to parse this to get title in php

I want to parse HTML code present in $raw to get the title and save it mysql. I have tried to do it with php dom and Ganon HTML parser but when I run it, shows me an error 500. it would be great if you solve this problem with Ganon.
function store($raw)
{
include_once('ganon.php');
$html = file_get_dom($raw);
echo $html('title', 0)->parent->getPlainText();
}
store ('<html> all html code </html>');
There are a few problems with your code.
Firstly you use file_get_dom() which is expecting to be passed in a file name, so usestr_get_dom() instead.
Secondly, the example HTML doesn't contain a title, so this won't work.
Then when you find the title, you go to the parent element and output from there. You just need to use that nodes content.
include_once('ganon.php');
function store($raw)
{
$html = str_get_dom($raw);
echo $html('title', 0)->getPlainText();
}
store ('<html><title>Title</title> all html code </html>');
outputs...
Title of page

PHP Simple HTML DOM Parser returning div as an array

I'm using PHP and simple HTML DOM Parser to try and grab song lyrics from a website. The song lyrics are held in a div with the class "lyrics". Here's the code I'm using to try and grab the div and display it. Currently it only returns "Array" onto my webpage. When I jsonify the array I can see that the array is empty.
<?php
include('simple_html_dom.php');
$data = file_get_contents("https://example.com/songlyrics");
$html = str_get_html($data);
$lyr = $html->find('div.lyrics');
echo $lyr;
?>
I know that the Simple HTML Dom Parser is being included correctly, and I have no problem displaying the full webpage when I echo $html with some small changes to the code, however I can't seem to echo just this div. Is there something wrong with my code? Why is $lyr returning an array?
There's nothing wrong with your code.
Why is $lyr returning an array?
It's because a class is considered to be used multiple times. If you var_dump($lyr) instead, you should see all the div-elements found with that class name.
You can either echo $lyr[0] or you can $html->find('div.lyrics',0) to select a specific div element.

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.

Form to pull page elements using file_get_contents and getElementsByClassName in PHP

I'm attempting to create a page where I input a url and the PHP code uses that to pull page elements from another website to be displayed on my blog post. I haven't even made it as far as the form, right now I just need to understand how to get this code to work so that it displays the page elements within the div with the class "products-grid first odd".
<?php
$homepage = file_get_contents('website');
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$dochtml->getElementsByClassName('products-grid first odd');
echo ????
?>
The PHP DOMDocument object does not appear to have the method getElementsByClassName().
Instead, I think you would have to getElementsByTagName() and then loop through those DOMElements and getAttribute('class') on each and check until you find the right one.

Categories