php - get a link's value - php

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

Related

Simple html dom parser - find value in attribute

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

Simple HTML DOM get value "data-url" from <div> tag

I'm trying to get the value from "data-url" from tags like this:
<div class="event event-list " data-id="24692" data-url="https://www.example.com/events/20-01-2017-event-x/">
this code is not working:
$urlVariable = ('https://www.example.com/');
$html = file_get_html($urlVariable);
foreach($html->find('[data-url]') as $detailLinks)
print $detailLinks . '<br>';
any help would be greatly appreciated.
You find div with data-url attribute
foreach($html->find('div[data-url]') as $detailLinks)
echo $detailLinks->{'data-url'};
You can achive the desired result using domxpath too in the following way:
<?php
$html = '<div class="event event-list " data-id="24692" data-url="https://www.example.com/events/20-01-2017-event-x/">';
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
print $xpath->query('//div[starts-with(#class,"event")]')->item(0)->getAttribute('data-url');
or
print $xpath->query('//div[contains(#class,"event-list")]')->item(0)->getAttribute('data-url');

How to replace getElementsByTagName() By document.getElementsById()

I have this code and I want to get a link of an image stored in a website by its Id but this code use getElementsByTagName('') :
<?php
$html = file_get_contents('http://example.com/dir/webpage.html');
$dom = new DOMDocument;
#$dom->loadHTML($html);
$links = $dom->getElementsByTagName('img');
foreach ($links as $link){
echo $link->nodeValue;
echo $link->getAttribute('href'), '<br>';
}
?>
And The HTML is:
<a href="/images/image1.png" id="img_1_id">
<div class="download"></div>
</a>
I want to replace getElementsByTagName('img') with document.getElementsByById(img_1_id)
so the script get the url of the selected image with the id: img_1_id
If there another way / code to do this please post it :)
Thank you pros!
getElementById returns a single element, you don't need a loop.
$link = $dom->getElemebtById('img_1_id');
echo $link->nodeValue;
echo $link->getAttribute('href');
BTW, img elements don't have an href attribute, they have src. They also don't have anything in their nodeValue, since <img> is not a container element.
you have to put the "
document.getElementsByById("img_1_id");
sou you get the element with id = "img_1_id"
what about this?
<?php
$html = file_get_contents('http://example.com/dir/webpage.html');
$dom = new DOMDocument;
#$dom->loadHTML($html);
$links = $dom->getElementById('img_1_id');
foreach ($links as $link){
echo $link->nodeValue;
echo $link->getAttribute('href'), '<br>';
}
?>

PHP DOM Document not parsing / retrieving HTML

I wrote the following:
<?php
$str = 'http://stackoverflow.com';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
//get all H1
$items = $DOM->getElementsByTagName('h1');
//display all H1 text
for ($i = 0; $i < $items->length; $i++)
{
echo $items->item($i)->nodeValue . "<br/>";
}
?>
And just wanted to simply retrieve all the H1 elements of stackoverflow, but can't get it working. Whenever I try filling in the variable $str manually (for example: <h1>hello</h1><div><h1>hello2</h1></div>) it is working. But whenever I try to parse content from another webpage it is not doing anything at all...
Help would be appericiated!
$str = 'http://stackoverflow.com';
$DOM = new DOMDocument;
$DOM->loadHTMLFile($str); // get html
echo $DOM->saveHTML(); echo html
$DOM->saveHTMLFile(FILE_NAME); save html to file

Reading XML POST data from URL

I am working with a 3rd party SMS supplier which they are sending me the delivery report of the SMS via URL as below:
http://www.mydomain.com/dlr.php <DeliveryReport><message id="024042313063119191" sentdate="2014/04/23 15:06:31" donedate="2014/04/23 15:06:35" status="DELIVERED" gsmerror="0" price="7.0" /></DeliveryReport>
And i am trying to read the XML data in dlr.php like below:
<?php
// read raw POST data
$postData = file_get_contents("php://input");
$dom = new DOMDocument();
$dom->loadXML($postData);
// create new XPath object for quering XML elements (nodes)
$xPath = new domxpath($dom);
// query “message” element
$reports = $xPath->query("/DeliveryReport/message");
// write out attributes of each “message” element
foreach ($reports as $node) {
echo “<br>id: “ . $node->getAttribute('id');
echo “<br>sent: “ . $node->getAttribute('sentdate');
echo “<br>done: “ . $node->getAttribute('donedate');
echo “<br>status: “ . $node->getAttribute('status');
echo “<br>gsmerrorcode: “ . $node->getAttribute('gsmerrorcode');
}
?>
I am getting this error:
Warning: DOMDocument::loadXML(): Empty string supplied as input in dlr.php
Any help how can I read the posted data correctly.
Thanks,
You can simply use this function for getting data from XML
function getFeed($feed_url)
{
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
foreach($x->channel->item as $entry) : ?>
<?php
$pdate = $entry->pubDate;
$pdate = rtrim($pdate,' -500');
$pdate = explode(', ',$pdate);
?>
<div >
<a href="<?php echo $entry->link; ?>" target="_blank">
<span > <?php echo $entry->title;?></span></a> <?php echo $pdate[1]; ?>
</div>
<?php
endforeach;
}
getFeed("// Your URL");

Categories