html divs into php array - php

I have this html code:
<div class="moviefilm">
SURSA 1
</div>
<div class="moviefilm">
SURSA 2
</div>
<div class="moviefilm">
SURSA 3
</div>
I want a php script to extract content of each and push him to php array to use with function
foreach ($divs as $div) {
//PHP CODE
}

you can do this with javascript getElementsByClassName then pass it to php by AJAX or something

Related

Extract Nested Tag Using PHP

Tag hierarchy in a webpage :
<body>
<div id='header'>
<h2>.....</h2>
</div>
<div id='main'>
<h2>...</h2>
//Some other content
<h2>...</h2>
</div>
<div id='footer'>
<h2>.....</h2>
</div>
</body>
[PROBLEM : ] From the above hierarchy structure of a webpaege, I want to extract only the <h2> tags which are inside the <div id='main'>. Can someone please please help me out ?
What I have tried is.... using HTML DOM of php $h2Tags = $htmlDom->getElementsByTagName('h2');, but this gives me all the <h2> tag which are outside of main div as well. Please guide me to a solution.
I have updated this to PHP:
h2_tags below will get list of h2s in main div:
$div_m = $htmlDom->getElementById('main');
$h2_tags = $div_m->getElementsByTagName('h2');
This is JS:
var div_m = document.getElementById("main");
var h2_tag = div_m.getElementsByTagName('h2');

Extract data from a website with simplehtmldom

I'm trying to extract some data from HTML DIV using Simple HTML Dom but it's not working right.
When I try to extract data from links/imgs it works fine but I have only a problem with DIVs.
HTML code:
<div class="price-container">
<div class="price-calc-container" id="attributes-calc-price">
<div class="current-price-container">
7,99 EUR
</div>
</div>
</div>
I want to extarct the number inside this div
$html = file_get_html("https://www.test.de/");
$preise = $html->find('div.current-price-container');
print_r($preise);
Sometime I will get an empty array sometimes nothing.
You have to start iterating over the find() result or use the last parameter to specify an index
$html = file_get_html("https://www.led-centrum.de/LED-Leuchtmittel/led-strahler/LED-Strahler-GU10/4058075160071.html");
$preise = $html->find('div.current-price-container', 0);
print_r($preise);
You can use a simple Jquery command like this
Try this
<div class="price-container">
<div class="price-calc-container" id="attributes-calc-price">
<div class="current-price-container">
7,99 EUR
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
var x = $('.current-price-container').text();
console.log(x);
</script>

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;
}

copy a complete section in HTML/PHP

<section id="123">
<div id="456">
some Data Here
</div>
</section>
I am a beginner to this and I need to copy data from section and div with their respective ids and use it some where else in my code. Please Help. Thanks for your time.
You can use
var htmlstring = $("#123").html();
.html() Function in jquery
Put your HTML in a php variable , and use it as you want
$div = '<section id="123">
<div id="456">
some Data Here
</div>
</section>';

Get content of multiple div tags with same class name

I read this article - Get DIV content from external Website . I get source of website with file_get_contents() function and I have to extract from it content of two divs with same class name.
I have very similar problem, but with divs with same class name. E.g. I have code like that:
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
I want to get both content of both these divs. Solution accepted in article I linked support only one. My expected result is array like this:
$divs[0] = "Some conete"
$divs[1] = "Second Content"
Please give me advice what to do. I read about DOMDocument class, but have no idea how to use it.
i have used the simple html dom parser and your content can be extracted as
$html = file_get_html('your html file link');
$k=1;
foreach($html->find('div.baaa') as $e){
$divs[$k]=$e;
$k++;
}
echo $divs[1]."<br>";
echo $divs[2];
You could use XPath. XPath is a query language for XML. There are PHP functions that support Xpath.
For you the example could be:
File test.html:
<html>
<body>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>
</body>
</html>
The php code that extracts contents of divs with the class "baaa"
$xml = simplexml_load_file('test.html');
$data = $xml->xpath('//div[#class="baaa"]/text()');
foreach($data as $row) {
printf($row);
}
generates the following output:
Some conete
Second Content
Look for XPath tutorials if you need more complex searching or analyzing.
Try it with your data:
$file_contents = file_get_contents('http://address.com');
preg_match_all('/<div class=\"baaa\">(.*?)<\/div>/s',$file_contents,$matches);
print_r($matches);
BTW: Polska rządzi :)
<script type="text/javascript">
$(document).ready(function(){
$('.baaa').each(function(){
alert($(this).text());
});
});
</script>
<div class="baaa">
Some conete
</div>
<div class="baaa">
Second Content
</div>

Categories