Get date from DOM element - php

I am trying to fetch date 01/08/14 from child information with Simple HTML DOM .
My code is:
HTML:
<div id="display_cr" class="loose-spacing tid">
<div class="review_by">By <strong>juand1</strong> on 01/08/14 06:51 AM (PST)</div>
<div class="review_by">By <strong>juand1</strong> on 01/08/14 06:51 AM (PST)</div>
</div>
PHP:
protected function scrap_date($review) {
$res = '';
$date = $review->find('div[class=review_by]');
if (isset($date)) {
foreach ($date as $div) {
$data = '';
foreach ($review->find('div[class=review_by]') as $element) {
$data = $element->outertext;
}
$res[] = $data;
}
}
return $res;
}

Try
foreach ($review->find('div[class=review_by]') as $element) {
$data = $element->innertext;
preg_match("/[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{1,2}/", $data ,$match);
if(sizeof($match) > 0 ){
res[] = $match[0];
}
}

Related

Using DomDocuments, finding and returning value of ID

I have the jquery that i can run and console and finds the element.
$.get("http://www.roblox.com/groups/group.aspx?gid=2755722", function(webpage) {
if ($(webpage).find("#ctl00_cphRoblox_rbxGroupFundsPane_GroupFunds .robux").length) {
alert("Eureka I found it!")
} else {
alert("nope!")
}
})
<div id="ctl00_cphRoblox_rbxGroupFundsPane_GroupFunds" class="StandardBox" style="padding-right:0">
<b>Funds:</b>
<span class="robux" style="margin-left:5px">29</span>
<span class="tickets" style="margin-left:5px">45</span>
</div>
When i try to run it as PHP with functions and using DomDocuments to handle it all, it wont return anything when i decode it. (the following is all part of a class)
protected function xpath($url,$path)
{
libxml_use_internal_errors(true);
$dom = new DomDocument;
$dom->loadHTML($this->file_get_contents_curl($url));
$xpath = new DomXPath($dom);
return $xpath->query($path);
}
public function GetGroupStats($id)
{
$elements = array (
'Robux' => "//span[#id='ctl00_cphRoblox_rbxGroupFundsPane_GroupFunds .robux']",
'Tix' => "//span[#id='ctl00_cphRoblox_rbxGroupFundsPane_GroupFunds .tickets']",
);
$data = array();
foreach($elements as $name => $element)
{
foreach ($this->xpath('http://www.roblox.com/Groups/group.aspx?gid='.$id,$element) as $i => $node)
$data[$name] = $node->nodeValue;
}
return $data;
}
//File that includes the class and runs the function (ignore the login stuff because it isn't required for this situation)
<?php
$randomstuffdude = include 'RApi.php';
$GetAccessToken = $_GET['token'];
if ($GetAccessToken == "secrettoken6996") {
$rbxBot = new Roblox();
$rbxBot -> DoLogin();
$StatsArray = $rbxBot->GetGroupStats(2755722);
foreach ($StatsArray as $other => $array) {
echo $other . ' : ' . $array . ' / ';
}
} else {
echo "no";
}
?>

How can I check If I get empty file_get_html in PHP HTML Dom?

I am getting JSON data through visiting a link using PHP HTML DOM, but sometimes, I get an empty page so I want to know that how can I really check if page is empty so that I can skip it by using continue in for loop
I am checking it through :
if (empty($jsondata))
But I always get TRUE never gets false even if page is returned empty
Here is my code :
<?php
$prefix = $_POST['prefix'];
$start_product = $_POST['start_product'];
$end_product = $_POST['end_product'];
set_time_limit(0);
for ($i=$start_product; $i <= $end_product; $i++) {
include('simple_html_dom.php');
$prefix ="00";
$i= "11";
$jsondata = file_get_html('http://www.ewallpk.com/index.php?controller=search&q=A'.$prefix.$i.'&limit=10&timestamp=1445547668758&ajaxSearch=1&id_lang=1');
if (!empty($jsondata)) {
$data = json_decode($jsondata, true);
$product = file_get_html($data[0]["product_link"]);
$product_name= "";
foreach($product->find('div[id=pb-left-column] h1') as $element) {
$product_name.=$element->innertext . '<br>';
}
$product_name = explode("_", $product_name);
$count = count($product_name);
if ($count < 3) {
$product_name=$product_name[0];
} else {
$product_name = "Error";
}
$product_description= "";
foreach($product->find('div[id=short_description_content]') as $element) {
$product_description.=$element->plaintext . '<br>';
}
$product_price= "";
foreach($product->find('p[class=our_price_display] span') as $element) {
$product_price.=$element->innertext . '<br>';
}
$image_link= "";
foreach($product->find('img[id=bigpic]') as $element) {
$image_link.=$element->src;
}
$content = file_get_contents($image_link);
file_put_contents('item_images/A'.$prefix.$i.'.jpg', $content);
echo "<strong>Product No : </strong> A".$prefix.$i."</br>";
echo "<strong>Product Name : </strong>".$product_name."</br>";
echo "<strong>Product Description : </strong>".$product_description;
echo "<strong>Product Price : </strong>".$product_price."</br></br></br>";
} else {
continue;
}
}
?>
You're probably getting some whitespace in the empty response, so trim it off before testing. You also should be using file_get_contents, since the response is not HTML.
$jsondata = file_get_contents('http://www.ewallpk.com/index.php?controller=search&q=A'.$prefix.$i.'&limit=10&timestamp=1445547668758&ajaxSearch=1&id_lang=1');
$jsondata = trim($jsondata);
if (!empty($jsondata)) {
...
}

get value using DOMDocument

I am trying to fetch a value from the following html snippet using DOMDocument:
<h3>
<meta itemprop="priceCurrency" content="EUR">€
<meta itemprop="price" content="465.0000">465
</h3>
I need to fetch the value 465 from this code snippet. To avail this I am using the following code:
foreach($dom->getElementsByTagName('h3') as $h) {
foreach($h->getElementsByTagName('meta') as $p) {
if($h->getAttribute('itemprop') == 'price') {
foreach($h->childNodes as $child) {
$name = $child->nodeValue;
echo $name;
$name = preg_replace('/[^0-9\,]/', '', $name);
// $name = number_format($name, 2, ',', ' ');
if (strpos($name,',') == false)
{
$name = $name .",00";
}
}
}
}
}
But this code is not fetching the value...can anyone please help me on this.
You have an invalid HTML. Where is the closing tag for meta? This is why you get the results you see.
To find what you are looking for you can use xpath:
$doc = new \DOMDocument();
$doc->loadXML($yourHTML);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("//meta[#itemprop='price']");
echo $elements->item(0)->textContent;
Inside your loop, you're pointing in the wrong object:
foreach($h->childNodes as $child) {
// ^ its not supposed to be `$h`
You should point to $p instead.
After that just use your current condition, if it satisfies, then loop all the child nodes:
$price = '';
foreach($dom->getElementsByTagName('h3') as $h) {
foreach($h->getElementsByTagName('meta') as $p) {
if($p->getAttribute('itemprop') === 'price') {
foreach($h->childNodes as $c) {
if($c->nodeType == XML_TEXT_NODE) {
$price .= trim($c->textContent);
}
}
if(strpos($price, ',') === false) {
$price .= ',00';
}
}
}
}
Sample Output
Another way is to use xpath queries:
$xpath = new DOMXpath($dom);
$meta = $xpath->query('//h3/meta[#itemprop="price"]');
if($meta->length > 0) { // found
$price = trim($xpath->evaluate('string(./following-sibling::text()[1])', $meta->item(0)));
if(strpos($price, ',') === false) { $price .= ',00'; }
$currency = $xpath->evaluate('string(./preceding-sibling::meta[#itemprop="priceCurrency"]/following-sibling::text()[1])', $meta->item(0));
$price = "{$currency} {$price}";
echo $price;
}
Out
Use jQuery, like this:
var priceCurrency = $('meta[itemprop="priceCurrency"]').attr("content");
var price = $('meta[itemprop="price"]').attr("content");
alert(priceCurrency + " " + price);
Outputs:
EUR 465.0000
CODEPEN DEMO

Remove array from JSON decode

Here's the function I'm using to grab and process a JSON input:
<?php
$json = "http://pastebin.com/raw.php?i=ihAapq30";
$cache_lastfm = 'BLAHBLAHDIR/'.sha1($json).'.json';
if(file_exists($cache_lastfm) && filemtime($cache_lastfm) > time() - 1000){
// if a cache file newer than 1000 seconds exists, use it
$data = json_decode(file_get_contents($cache_lastfm), true);
} else {
$data = json_decode(file_get_contents($json), true);
file_put_contents($cache_lastfm,json_encode($data));
}
$data = $data['recenttracks'];
foreach ($data['track'] as $track) {
$artist = $track['artist']['#text'];
$title = $track['name'];
$url = $track['url'];
echo '<li>', $artist, ' - ', $title, '</li>'; }
?>
It works perfectly.. my question is, how can I remove only the "entry" that has the:
"#attr":{
"nowplaying":"true"
}
... "attribute"? Check the pastebin page to understand what I mean :)
Please try this:
<?php
$data = $data['recenttracks'];
$tracks=$data['track'];
foreach ($tracks as $index=>$track) {
if (isset($track['#attr'])) {
unset($tracks[$index]);
}
}
foreach ($tracks as $track) {
$artist = $track['artist']['#text'];
$title = $track['name'];
$url = $track['url'];
echo '<li>', $artist, ' - ', $title, '</li>';
}
?>

First and Second last xml nodes

I only want the first and the second last Area nodes - how would I do that here?
$url = "http://developer.multimap.com/API/geocode/1.2/OA10081917657704697?qs=Byker&countryCode=GB";
$results = simplexml_load_file($url);
foreach($results->Location as $location) {
echo "<hr />";
foreach($location->Address as $address) {
foreach($address->Areas as $areas) {
foreach($areas->Area as $area) {
echo $area;
echo "<br />";
}
}
}
}
Update: If you have those foreach-loops anyway you can simply use:
$url = "http://developer.multimap.com/API/geocode/1.2/OA10081917657704697?qs=Byker&countryCode=GB";
$results = simplexml_load_file($url);
foreach($results->Location as $location) {
foreach($location->Address as $address) {
foreach( $address->Areas as $areas) {
// <-- todo: add test if those two elements exist -->
echo $areas->Area[0], ' - ', $areas->Area[count($areas->Area)-1], "\n";
}
}
}
You can use XPath for this.
<?php
$doc = new SimpleXMLElement('<foo>
<bar>a</bar>
<bar>b</bar>
<bar>c</bar>
<bar>x</bar>
<bar>y</bar>
<bar>z</bar>
</foo>');
$nodes = $doc->xpath('bar[position()=1 or position()=last()-1]');
foreach( $nodes as $n ) {
echo $n, "\n";
}
prints
a
y
see also:
PHP Manual: SimpleXMLElement::xpath()
XPath: predicates
XPath: position()
XPath: last()
Here it is:
<?php
$url = 'http://developer.multimap.com/API/geocode/1.2/OA10081917657704697?qs=Byker&countryCode=GB';
$results = simplexml_load_file($url);
$areas = array();
foreach ($results->Location->Address->Areas->Area as $area)
{
$areas[] = (string) $area;
}
$first = $areas[0];
$second_last = $areas[count($areas)-2];
?>

Categories