Simple html dom parser - find value in attribute - php

<div class="bk-cell-wrapper">
<div class="bk-timetable-cell">
div class="day-item-hover" data-detail="{**value i want to find**}" >BlaBlaBlabLa</div>
</div>
</div>
from this pattern which repeats multipletimes, i want to extract all divs with attribute "data-detail" in it.
I made it with this code :
$html = file_get_html($url);
foreach($html->find('div[data-detail]') as $element )
echo $element
now i want to extract the value in attribute "data-detail" from the variable $element, where i store each div with attr "data-detail" in it
view-source:https://oa-poruba.bakalari.cz/Timetable/Public/Actual/Class/WV

You can do it like this:
$html = file_get_html($url);
$myDiv = $html->find('div[data-detail]');
foreach($myDiv as $element ) {
echo $element->getAttribute('data-detail');
}

you can use this package php-html-parser
and get what you want like this:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadStr('<div class="all"><p>Hey bro, click here<br /> :)</p></div>');
$a = $dom->find('a')[0];
echo $a->text; // "click here"
And also get the attribute like this:
// Assuming you installed from Composer:
require "vendor/autoload.php";
use PHPHtmlParser\Dom;
$dom = new Dom;
$dom->loadFromFile('tests/data/big.html');
$contents = $dom->find('.content-border');
echo count($contents); // 10
foreach ($contents as $content)
{
// get the class attr
$class = $content->getAttribute('class');
}

Related

How to get value out of custom html tags from DOMDocument or file_get_content

I am wondering how I can get value out of custom HTML tags in PHP form URL, for example:
<key>1234</key>
I've tried like that:
$html = file_get_html("https://example.com");
foreach($html->find('key') as $element) {
echo $element;
}
And like that:
$site = file_get_contents("https://example.com");
$dom = new DOMDocument();
#$dom->loadHTML($site, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$data = $dom->getElementsByTagName("key");
$html = $dom->saveHTML($data);
echo $html;
But without result. ;/
If you want to access the text inside an element, you have to use something like: this.
$html = file_get_html("http://randomurl.com");
// First element
$element = $html->find("key",0); // The first <key> element
$text = $element->plaintext; // The text
// Mutilple elements
foreach($html->find("key") as $element){
print($element->plaintext);
}
Have you tried strip_tags($html) to remove the HTML tags?

get value of href inside of div from external site using PHP

good day Sir/Maam.
I have a certain html attribute that I want to search from the external website
I want to get the a href value but the problem is the id or class or name is random.
<div class="static">
Dynamic
</div>
This code should display all the hrefs in http://example.com
In this case I use DOMDocument and XPath to select the elements you want to access because it's very flexible and easy to use.
<?php
$html = file_get_contents("http://example.com");
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DomXPath($doc);
$nodeList = $xpath->query("//a/#href");
print_r($nodeList);
// To access the values inside nodes
foreach($nodeList as $node){
echo "<p>" . $node->nodeValue . "</p>";
}
use jquery to get the value as follow:
var link = $(".static>a").attr("href");
You can use PHP DOMDocument:
<?php
$exampleurl = "http://YourDomain.com"; //set your url
$filterClass = "dynamicclass";
$dom = new DOMDocument('1.0');
#$dom->loadHTMLFile($exampleurl);
$anchors = $dom->getElementsByTagName('a');
foreach ($anchors as $element) {
$href = $element->getAttribute('href'); // all href
$class = $element->getAttribute('class');
if($class==$filterClass){
echo $href;
}
}
?>

How to find element in already parsed HTML data

Here I have a very simple code to grab all the 'div' elements with the classname 'info_block'. I am wondering how would I go about finding another element with the classname 'price' from within 'info_block' and display it instead of the whole 'info_block' element.
Main Goal: Find the price in each element with classname 'info_block'. but do inside the foreach, because I may need to find other elements.
<?php
$page = file_get_contents('example.com');
$dom = new DOMDocument();
$dom->loadHTML($page);
$xpath = new DOMXPath($dom);
$div1 = $xpath->query('//div[#class="info_block"]');
foreach ($div1 as $var1){
//echo $dom->saveHTML($var1);
}
?>
There is a element in each of the 'info_block' with a classname 'price' and I would like to display only that element. Like so...
foreach ($div1 as $var1){
$dom2 = new DOMDocument();
$dom2->loadHTML($dom->saveHTML($var1));
$xpath2 = new DOMXPath($dom2);
$div2 = $xpath2->query('//div[#class="price"]');
$div2 = $div2->item(0);
echo $dom2->saveHTML($div2);
}
But instead of just giving me the price it returns the whole HTML for 'info_block' as it did before.
You could provide each <div class="info_block"> found and search for <div class="price">" by providing it in the second argument of ->query():
$div1 = $xpath->query('//div[#class="info_block"]');
foreach ($div1 as $var1){
$div2 = $xpath->query('./div[#class="price"]', $var1);
// ^ each div
$div2 = $div2->item(0);
echo $dom->saveHTML($div2);
}
Note: You do not need to create another instance of DOM and DOMXpath.
This example is taken into context of this kind of HTML semantic:
<div class="info_block"> // each info block
<div class="price">1</div> // inside of it has price
</div>
<div class="info_block">
<div class="price">2</div>
</div>
You can combine queries in XPath to find all the desired elements in one go
$xpath->query('//div[#class="info_block"]|//div[#class="price"]');
You can specify dom elements for doing relative XPath queries. Its optional in xpath->query method
<?php
$page = file_get_contents('example.com');
$dom = new DOMDocument();
$dom->loadHTML($page);
$xpath = new DOMXPath($dom);
$div1 = $xpath->query('//div[#class="info_block"]');
foreach ($div1 as $var1){
$div2 = $xpath2->query('//a[#class="price"]', $var1);
foreach ($div2 as $var2) {
echo $var2->nodeValue. "\n";
}
}
?>
For more you can see xpath documentation here
xpath query documentation

php - get a link's value

I'm using php to get a part of a html file:
HTML file:
<div class="titles">
<h2>First Title</h2>
</div>
PHP file:
<?php
include_once('simple_html_dom.php');
$url = 'http://example.com';
$html = file_get_html($url);
$titles = $html->find('.titles');
$heading = $titles->find('h2')[0];
$link = $heading->find('a')[0];
echo $link;
//result: First Title
?>
How can I separately get the value of href and 'a' tag?
Because I want to save the title and link into the database,
I need '#' and 'First Title' not the 'a' tag.
$link should be a Simple HTML Element object, of which you can access attributes using $link->href and the text contents as $link->plaintext. See http://simplehtmldom.sourceforge.net/manual.htm.
U can use DOMDocument and DOMXpath object's (>=php5)
ref: http://php.net/manual/en/class.domdocument.php
part of sample code:
$html = '<div class="titles">
<h2>First Title</h2>
</div>';
$page = new DOMDocument();
$page->loadHtml($html);
$xpath = new DOMXpath($page);
$a = $xpath->query("//a");
for ($i=0; $i < $a->length; $i++) {
$_a = $a->item($i);
echo $_a->getAttribute("href");
echo "<br>";
echo $_a->textContent;
}

how to get div class value using dom document

how to get div class value using dom document
i need to echo this value 4,458 members
from the below code
< div class="mbs fcg">4,458 members< /div>
right now my orginal code is
$links = $dom->getElementsByTagName('div');
foreach ($links as $link){
echo $link->nodeValue;
echo $link->getAttribute('class');
}
how to target this particular class = mbc fcg ?
now with my present code i am getting all div values.
what changes i should do
you will need to use DOMXPath, which will take a DOMDocument instance
$xpath = new DOMXPath( $dom );
// if the className doesn`t changes
$members = $xpath->query( '//div[#class="mbs fcg"]' );
// if the class name changes ex. class="mbs fcg my-other class-name"
$members = $xpath->query( '//div[contains(#class,"mbs fcg")]' );
alternatively if you want to iterate all over your div`s you could try
$divs = $dom->getElementsByTagName( 'div' );
foreach( $divs as $div ){
// if the className doesn`t changes
if( $div->getAttribute( 'class' ) === 'mbs fcg' ){
echo $div->nodeValue;
}
// if the class name changes ex. class="mbs fcg my-other class-name"
if( strpos( $div->getAttribute( 'class' ), 'mbs fcg' ) !== false ){
echo $div->nodeValue;
}
}
NOTICE:::
THIS IS A JAVASCRIPT SOLUTION ... NOT A PHP DOMDOCUMENT SOLUTION
Try this HTML:
<div id="ME" class="mbs fcg">4,458 members</div>
... and this Javascript:
var WANTED_TEXT = document.getElementById('ME').firstChild.nodeValue;
EDIT2:
If you actually want, to get all textnodes from all occurrences of elements having class='mbs cfg' ... then try the following HTML:
<div class="mbs fcg">4,458 members</div>
... and this Javascript:
var Collection = document.getElementsByClassName('mbs fcg');
for(i=0; i<Collection.length; i++) {
Texts = Collection[i].firstChild.nodeValue;
document.write('<p>'+Texts+'</p>');
}
That should echo the pure text from all elements in the Collection.
I think you need to use an id to target a single div, such as:
< div id="my_id_name" class="mbs fcg">4,458 members< /div>

Categories