First and Second last xml nodes - php

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];
?>

Related

Get link by id API/URL

I have this code
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
// To access the first one:
echo "\nFirst link = " . $items[0]->link . "\n";
I want to get link by id.
Can anyone help?
More ore less what you need could be:
<?php
// The result of the request:
$content = <<<END_OF_STRING
[{"id":"2972","name":"MBC 1","link":"http://46.105.112.116/?watch=TR/mbc1-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC1En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc1.png"},{"id":"1858","name":"MBC 2","link":"http://46.105.112.116/?watch=TN/mbc2-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/MBC2En.ae-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc2.png"},{"id":"1859","name":"MBC 3","link":"http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1","epg":"https://epg.cdnrdn.com/-20220201.xml","dvr":"disabled","language":"Arabic","category":"TOP 100","logo":"http://files.rednetcontent.com/chlogo/mbc3.png"}]
END_OF_STRING;
$items = json_decode($content);
$id = 1859;
$desiredLink = '';
foreach ($items as $item) {
if ($id == $item->id) {
$desiredLink = $item->link;
}
}
// To access the first one:
echo "\nDesired link = " . $desiredLink . "\n";
This will output: Desired link = http://46.105.112.116/?watch=TN/mbc3-ar&token=RED_cexVeBNZ8mioQnjmGiYNEg==,1643770076.5266113827&t=1&s=2&p=1&c=BR&r=1351&lb=1
If you prefer, ... I've also implemented a little class to do the same job:
class Finder {
public function __construct(private string $content) {}
public function getLinkById(int $id) {
$items = json_decode($this->content);
foreach ($items as $item) {
if ($id == $item->id) {
return $item->link;
}
}
}
}
// To access the first one:
echo "\nDesired link = " . (new Finder($content))->getLinkById($id) . "\n";

Getting XML node names without duplicating in simplexml_load_file

I'm getting the node names of an XML using this code:
$url = 'https://www.toptanperpa.com/xml.php?c=shopphp&xmlc=e7ef2a0122';
$xml = simplexml_load_file($url) or die("URL Read Error");
echo $xml->getName() . "<br>";
foreach ($xml->children() as $child) {
echo $child->getName() . "<br>";
foreach ($child->children() as $child2) {
echo $child2->getName() . "<br>";
foreach ($child2->children() as $child3) {
echo $child3->getName() . "<br>";
foreach ($child3->children() as $child4) {
echo $child4->getName() . "<br>";
}
}
}
}
I'm getting the nodes and children correctly, however, it's duplicating.
Result is as below:
urunler
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
urun
urun_aktif
urun_metaKeywords
urun_metaDescription
urun_url
Should I just use array_unique or is there a better method?
Thanks
i used recursive function this is simple
function getChildrens($x) {
$children = array();
array_push($children, $x->getName());
foreach ($x->children() as $chld) {
$children = array_merge($children, getChildrens($chld));
}
return array_unique($children);
}
echo implode("<br>", getChildrens($xml));

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

Get date from DOM element

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

PHP foreach with two 'as'?

Hi there i am trying to combine two loops of foreach but i have a problem.
The problem is that the <a href='$link'> is same to all results but they must be different.
Here is the code that i am using:
<?php
$feed = file_get_contents('http://grabo.bg/rss/?city=&affid=16090');
$rss = simplexml_load_string($feed);
$doc = new DOMDocument();
#$doc->loadHTML($feed);
$tags = $doc->getElementsByTagName('link');
foreach ($tags as $tag) {
foreach($rss as $r){
$title = $r->title;
$content = $r->content;
$link = $tag->getAttribute('href');
echo "<a href='$link'>$title</a> <br> $content";
}
}
?>
Where i my mistake? Why it's not working and how i make it work properly?
Thanks in advance!
Both loops were going through different resources so you are just simply cross joining all records in them.
This should work to get the data you need:
<?php
$feed = file_get_contents('http://grabo.bg/rss/?city=&affid=16090');
$rss = simplexml_load_string($feed);
foreach ($rss as $key => $entry) {
if ($key == "entry")
{
$title = (string) $entry->title;
$content = (string) $entry->content;
$link = (string) $entry->link["href"];
echo "<a href='$link'>$title</a><br />" . $content;
}
}

Categories