If a div has class "one-two-three" then - php

I'm currently working on a website. And I need to use PHP to select a div with a certain class. For example:
<body>
<div class="header"></div>
<div class="other-class"></div>
<div class="one-two-three"></div>
<div class="footer"></div>
</body>
<?php
if(div.hasClass('one-two-three'))
{
//function code here...
}
?>
But keep in mind that I want to use PHP and not jQuery for this...

If you want do manipulate the DOM, prior to sending it to the client then the Dom object is offers what you need. It even has similar methods to what you might know already from JS.
$htmlString = '<html>...</html>';//either generated, or loaded from a file
$dom = new DOMDocument;
$dom->loadHTML($htmlString);//or loadHTMLFile
$divs = $dom->getElementsByTagName('div');
foreach($divs as $div)
{
if ($div->hasAttribute('class') && strstr($div->getAttribute('class'), 'one-two-three'))
{
//$div ~= <div class='one-two-three'>
$div->appendChild();//check the docs to see what you can do
}
}
Here's an overview of the methods at your disposal

You probably will fill the div with content using php and then you can process your content first and then print it in the div. If you need to do stuff to the div's contents when the div's already in the browser you will need to use javascript to do it or else call a php script to do the processing using ajax to call the php script on the backend and retrieve the response.

use simplephpdom library for this. You can select form elements with selectors like you do in jquery.
http://simplehtmldom.sourceforge.net

Related

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.

Store and display scraped content from xml file

As of my previous question, I scraped the content and displayed in a html page using the code below:
<?php
include_once('simple_html_dom.php');
$target_url="http://www.amazon.in/gp/bestsellers/books/1318209031/ref=zg_bs_nav_b_2_1318203031";
$html = new simple_html_dom();
$html->load_file($target_url);
?>
<html>
<body>
<div style="margin:auto;width:900px">
<?php
foreach($html->find('div[class=zg_itemWrapper]') as $post)
{
echo $post;
}
?>
</div>
</body>
</html>
I want to store the same in an Xml file and display 10 items each time I scroll down the page using jQuery's window.scroll() function.
My question is how do I store this scraped data in Xml file for displaying? (instead of using a database or similar ways to store)I couldn't find any proper solution for doing the same.I'm new to using xml this way. An implementation would really help,
thank you
You need to create page that will send only post with page parameter that will print n page. and then you can use infinite ajax scrol jquery plugin.
UPDATE: Here is the code how to use the plugin, using this just create a php script that will have just the foreach loop with post just for n page.

Delete html elements with php

I was wondering if I had an HTML page, how would I delete an element with php, by the name or id of it. so I could have HTML code like this
<div>
<span id='todelete' name='todelete'>This should be deleted</span>
<?php
delete('todelete');
?>
</div>
or something like that, and the web browser would just show an empty div without the span.
Is that possible, or is it more useful to just use javascript for this.
Basically, what I'm saying is before the page is compiled, can you delete elements from it, based on some conditionals, not after the page has loaded
It appears you are getting confused between server side and client side scripting languages.
PHP is a server side language meaning that once it has been compiled that's it, it can't be changed dynamically in the users browser.
JavaScript on the other hand is a client side scripting language and can manipulate what is on the page dynamically.
Therefore what you are asking to do is impossible using pure PHP once the page has been rendered.
You can use AJAX technology to call a PHP script from within a page already in a browser using JavaScript. You can then use the PHP script being called by the AJAX request to return something that you can use to update a particular section of your page, but there is no pure PHP way that doesn't involve JavaScript at all to do this.
Just an idea... have you thought about PHP XPath Query?
// CSS not important
<style>
div { border:1px solid rgba(255,0,0,0.5); background:rgba(255,0,0,0.1) }
span{ border:1px solid rgba(0,255,0,0.5); margin:0px 2px; padding:0 20px;
background:rgba(0,255,0,0.5)}
</style>
// PHP your HTML, in this example a string.
<?php
$string = '
<div>
<span id="keep"> John </span>
<span id="keep"> Susane </span>
<span id="todelete"> Tree </span>
<span id="keep"> Karin </span>
<span id="keep"> Jack </span>
</div>
';
echo $string;
// PHP start new Class DOMDocument and make the query
$doc = new DOMDocument;
#$doc->loadHTML($string);
$xpath = new DOMXPath($doc);
$found = $xpath->query("//span[contains(#id, 'todelete')]");
// more PHP I can't explain this well. I hope someone else can?
// But The query found the span that contains attribute id with 'todelete'
// I guess it output it as HTML
$element = $found[0]->ownerDocument->saveXML($found[0]);
/* ##### <span id="todelete"> Tree </span> ##### */
echo '<p>delete:' . $element . '<p>';
//Finally PHP Replace in your string what you found with nothing
echo str_replace($element, '' , $string);
?>
I hope its useful. XPath is very powerful and often forgotten. Give it a try. I hope it's enough for your needs. you can copy all the code above and paste it in a .php document.
You can't use php for removing DOM elements. You need to do it with javascript or jQuery.
With javascript it will be so
var element = document.getElementById("todelete");
element.parentNode.removeChild(element);
And with jQuery (after including the library)
$(document).ready(function() {
$("#todelete").remove();
});
You can't really use php to delete the html once it has been rendered, but you can use conditions in your php code to selectively render certain sections of html depending on the outcome of tests you perform in php...

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.

Getting and placing content within html tag by its class using php

Is it possible to get and place content within an html tag by its class name?
For Example:
<div class='edit'>
Wow! I'm the Content.
</div>
Is it possible to get that value, edit and place it back or a new value to that div etc? If it's possible... will it work if it has multiple classes? Like:
<div class='span-20 edit'>
Wow! I'm the Content.
</div>
If you can determine which specific HTML tag to manipulate, you have various tools at your disposal. You can use str_replace, preg_replace, DOMDocument, DOMXPath, and simplexml in this situation.
If in PHP, try this:
$xhtml = simplexml_load_string("<div class='edit'>Wow! I'm the Content.</div>");
$divs = $xhtml->xpath('//div[#class=edit]');
if (!empty($divs){
foreach ($divs as $div){
$div['class'] .= ' span-20';
}
}
return $xhtml->asXML();
With jQuery javascript library, do this:
$('.edit').addClass('span-20');

Categories