Getting element in PHP - PHP Simple HTML DOM Parser - php

Can you help me with the resolution below?
I have the following code in html:
<div class="return-form">
<div class="two_cols">
<div class="first_col">
<label for="namesinger">Name:</label> </div>
<div class="second_col">
<p id="name">Axl Rose</p>
</div>
</div>
I am using the PHP Simple HTML DOM Parser library and I would like to display only the name "Axl Rose" on the screen.
echo ($ name)
expected exit
Axl Rose

This how you can extract the data
<?php
// Load the HTML
$html = str_get_html('<div class="return-form">
<div class="two_cols">
<div class="first_col">
<label for="namesinger">Name:</label> </div>
<div class="second_col">
<p id="name">Axl Rose</p>
</div>
</di');
// Locate the date via div ID and display
echo $html->find('p[id=name]', 0)->plaintext;
?>
For more details Read this

Related

Can anyone help me by getting The plain text from a div placed in an html file

i am a facing an error while using php simple html dom . I am working on my school project to get the plain text from a following div which is situated in an html file
<div class="product-detail__TextPrice-sc-1k47nh4-0 hECROB d-flex align-items-center justify-content-md-start">
<div class="mb-0 weight-bold price">
₹<!-- --> <!-- -->126.00
</div>
<div class="mrp ml-md-4 ml-lg-2 pd-mrp">
₹ 150.00
</div>
<div class=" w-50 margin-text ml-md-4 ml-lg-2">
<span class="main-slab main-slab-detail">
1+pc
</span>
Margin ₹ 24.00 | 16.00%
</div>
</div>
This div is present in an html file and we have got only the url to file and we have to first bring the mrp cost of item in div class mrp ml-md-4 ml-lg-2 pd-mrp and cost from class
mb-0 weight-bold price
I was using php simple html dom to do the process but i am getting a huge array and I can't figure out what to do now . Can anyone please guide me out ,. The code which i am using is below
<?php
include('simple_html_dom.php');
$cl=file_get_contents($url);
$html=new simple_html_dom();
$html->load($cl);
$ret = $html->find('div[class=mrp ml-md-4 ml-lg-2 pd-mrp]');
var_dump($ret);
?>
Thanks in advance
Try this script.
Because $html->find('div.pd-mrp') return multiple elements.
$html=new simple_html_dom();
$html->load($cl);
foreach($html->find('div.pd-mrp') as $elements) {
echo $elements->plaintext;
break;
}
Final answer :
The target value is a price on a product page. This value is not served at first call, so simple_html_dom can't access it. The workaround would be to scrape the frontend page in a browser extension for example, and to get the value via javascript. This way, you could get your value and process it or send it back to php, if you need.
First answer :
I've made a text text on my server :
<?php
include('simple_html_dom.php');
$cl = <<< TEST
<div class="product-detail__TextPrice-sc-1k47nh4-0 hECROB d-flex align-items-center justify-content-md-start">
<div class="mb-0 weight-bold price">
₹<!-- --> <!-- -->126.00
</div>
<div class="mrp ml-md-4 ml-lg-2 pd-mrp">
₹ 150.00
</div>
<div class=" w-50 margin-text ml-md-4 ml-lg-2">
<span class="main-slab main-slab-detail">
1+pc
</span>
Margin ₹ 24.00 | 16.00%
</div>
</div>
TEST;
$html=new simple_html_dom();
$html->load($cl);
$ret = $html->find("div.mrp.ml-md-4.ml-lg-2.pd-mrp",0)->plaintext;
print_r($ret);
?>
it outputs :
₹ 150.00

How to get content text of div by simple html dom - php

I get the bottom html code by simple dom html (file_get_html('http://example.com'))
<div id="ship" class="fe" data-feature-name="box" data-cel-widget="sox">
<div class="a-medium b-di">
<div id="mer-info" class="a-section a-spacing-mini">
Hello World
<span class="">
</span>
</div>
</div>
</div>
How can I get 'Hello World" content text?
I tried a lot of things for example bottom text, but that gave me 'NULL'
$html->find('div[id="mer-info"]',0);
$html->find("div#mer-info");
$html->find("div#mer-info")->plaintext;
$html->find('div[id="mer-info"]')->innertext;
and ...
But I got NULL still!
You only passed the second argument (0) to find method where you used div[id="mer-info"] as selector, which seems not to be recognized by find method. Try the following:
require 'simple_html_dom.php';
$html =<<<html
<div id="ship" class="fe" data-feature-name="box" data-cel-widget="sox">
<div class="a-medium b-di">
<div id="mer-info" class="a-section a-spacing-mini">
Hello World
<span class="">
</span>
</div>
</div>
</div>
html;
$dom = str_get_html($html);
$elem = $dom->find('#mer-info', 0);
print $elem->plaintext;
print "\n";
$elem = $dom->find('div#mer-info', 0);
print $elem->plaintext;

how to extract raw html code using simplehtmldom

I am trying to extract raw html from a web-page using simplehtmldom. I was wondering if it is possible using that library.
For example, let's say I have this web page I am trying to extract data from.
<div class="class1">
<div class="class2">
<div class="class3">
<p>p1</p>
<h1>header here!</h1>
<p>p2</p>
<img src="someimage"></img>
</div>
</div>
</div>
My goal is to extract everything within div class3 including the raw html code so when I get the data I can enter it to a text box which allows input for source code so it is formatted the same way it is from the webpage.
I have looked at simplehtmldom manuals and did some searching but have yet to find a solution.
Thank you.
Using your example html string
$html = str_get_html('<div class="class1">
<div class="class2">
<div class="class3">
<p>p1</p>
<h1>header here!</h1>
<p>p2</p>
<img src="someimage"></img>
</div>
</div>
</div>');
// Find all divs with class3
foreach($html->find('div[class=class3]') as $element) {
echo $element->outertext;
}

How to extract information between xml tag

I am trying to convert an xml code of the form
<document>
<section>
Some Text 1
<theorem>
Let $H$ be a real or complex Hilbert space.
</theorem>
Some Text 2
</section>
</document>
to html code
<div class="section">
<div class="text">
Some Text 1
</div>
<div class="theorem">
Let $H$ be a real or complex Hilbert space.
</div>
<div class="text">
Some Text 2
</div>
</div>
This is what done so far
$xml=simplexml_load_file("text.xml") or die("Error: Cannot create object");
foreach($xml->children() as $tags){
echo'<div class="section">';
echo '<div class="theorem">'.$tags->theorem.'</div>';
echo'</div>';
}
The problem is I am not sure how to extract Some Text 1 and Some Text 2 separately so I can echo each in <div class="text"></div> tag. Does anyone know how to fix this?

most acceptable way to put dynamic html on a page

I would like to append some html to a div when a button is clicked. The only thing that needs to be dynamic about it is that it needs to have a different id the the otherwise identical html above it. So if I have a structure like this:
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
and to the end of container i would like to append
<div id="div3" class="inner">
<p id="P3">Some content</p>
</div>
What would be a good method to store this HTML? Keep it all in a php page and post the number the new div's id should be incremented to? Or is is smarter to put the html inside a string, and have a regular expression increment all of the numbers. Or is there some incredibly obvious way to do this that I've completely missed.
Jquery is an option, as that's already on the page.
Also, if this question seems too open-ended, please let me know in the comments how I can change it before closing it.
Thank you very much.
Based on your answers to comments so far (i.e. that the id's aren't really important, and that you will have input elements in the content), I would say that you would want to redo your html to look like this:
<div id="container">
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
<div class="inner">
<p><input type="text" name="order_item[]" value="" /></p>
</div>
</div>
Note there are no longer id's and I have used array notation for your input elements such that they will be posted as an array to the receiving script (eliminating the need to increment counters in javascript or parse different posted variable names into a usable array in PHP on the server).
And then use something like this in jQuery
$('#button_id').click(function() {
$('#container > div.inner:last').clone().val('').appendTo('#container');
});
Put the HTML you want to add each time into a hidden div, then use jQuery to copy and add that to the relevant location. For instance:
<div id="appendContents" style="display:none;">
<div class="inner">
<p id="P3">Some content</p>
</div>
</div>
then jquery:
var shownDivs = <?php echo $numberOnScreenFromBeginning;?>;
$('#theButtonYouWantClickable').click(function()
{
var newContent = $('#appendContents').clone();
newContent.$('div.inner').get(0).id = 'div'+shownDivs;
newContent.$('div.inner p').get(0).id = 'P'+shownDivs;
// Where does P content come from??
shownDivs++;
$('#container').append(newContent);
});
Try this Please: Working Demo http://jsfiddle.net/eNXmr/
In the code below it will calculate the lenght of the div hence dynamically set the id as 1, 2, 3, and beyond.
Hope it fits you need :)
API used: http://api.jquery.com/append/
Code
$('#hulk').click(function() {
alert("Total div inside ==> " + $('#container').find('.inner').length);
var next_num = parseInt($('#container').find('.inner').length) + 1;
$('#container').append(' <div id="div' + next_num + '" class="inner"><p id="P' + next_num + '">Some content</p></div>');
});​
​
HTML
<div id="container">
<div id="div1" class="inner">
<p id="P1">Some content</p>
</div>
<div id="div2" class="inner">
<p id="P2">Some content</p>
</div>
</div>
<input type="button" value="Click hulk" id="hulk" />​

Categories