How I can extract field to xml in Php? [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a xml with more subcategory.
I want extract in Php the “long_name” where type is “adminkstrative_area_level_3”
How I can do?
This is my xml https://ibb.co/fY27bJ
I tried but don’t work
<?
$string_data = "https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI";
$xml = simplexml_load_string($string_data);
$citta = (string) $xml->result[0]->address_component[3]->long_name;
echo "<p>".$citta."</p>";
?>

You are missing geoname in $xml-> name;
Try it like this:
$xml = simplexml_load_string($string_data);
$citta = (string)$xml->geoname->name;
echo $citta;
Demo Php
If you want to loop through mulitple items you could use:
foreach ($xml->geoname as $item) {
echo $item->name;
}
Update:
For the updated part you could use the same technique:
$xml = simplexml_load_file("https://maps.googleapis.com/maps/api/geocode/xml?latlng=41.51,15.16&key=AIzaSyClG_vc2nkQCzXqvDzW1maPrUWLyADI7xI");
foreach ($xml->result as $item) {
if ((string)$item->type === "administrative_area_level_3") {
echo $item->address_component->long_name;
}
}
Or by index [1]:
echo $xml->result[1]->address_component->long_name;

Related

Get the HREF from a link with a specific class [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
<a data-track='' _sp= class=s-item__link href=get_this_href>...</a>
With the above link, the data-track contains some json data. The _sp= could contain numbers/letters and a period (.). The class is s-item__link.
I would need the get_this_href and then I can go from there.
This is the regex I tried... but im stuck from here.
<a\b(?=[^>]* class="[^"]*(?<=[" ])s-item__link[" ])(?=[^>]* href="([^"]*))
Here is an example: https://regex101.com/r/rVPeUI/1
$link = ""; //url im scraping
$html = file_get_html($link);
//find is part of simple_html_dom.php. im saying each li item is an $item.
foreach ($html->find('li.s-item ') as $item) {
//$item contains the decent amount of nested divs with spans and links.
}
Without using Regex, its better to use DOMDocument() to parse HTML tags:
$doc = DOMDocument::loadHTML($html);
$xpath = new DOMXPath($doc);
$query = "//a[#class='s-item__link']";
$entries = $xpath->query($query);
foreach ($entries as $entry) {
echo "HREF " . $entry->getAttribute("href");
}

Get content from remote html page [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking for a way to get specific content from a remote web page
The content I want to get are inside javascript variables, this kind :
var Example1 = 0;
var Example2 = 14;
The name of the variable remain the same and the content is only numbers
Thank you
Find scripts in html source by DomDocument and then variable declaration by regex
$DOM = new DomDocument();
$DOM->loadHTML( $output);
$res = [];
$scripts = $DOM->getElementsByTagName('script');
$lnt = $scripts->length;
for($i=0; $i < $lnt; $i++) {
preg_match_all('/var\s+(\w+)\s*=\s*(\d+)\s*;/', $DOM->saveHtml($scripts->item($i)), $m);
$res = array_merge($res, array_combine($m[1], $m[2]));
}
print_r($res);
demo

unable to a decode_json in php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have A Json data But I am unable to get desired response.
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
i want to capture some contents like
id , msisdncountrycode , msisdncountrycode , statuscode , hlrerrorcodeid
And Other Values As Well Please Help To Solve This problem .
check out this:
$result = json_decode($content);
var_dump($result->results[0]->id);//will get id
var_dump($result->results[0]->msisdncountrycode);//will get Country code
use this(works with multiple results).
$d = json_decode($content);
foreach($d->results as $row){
echo $row->id; //echoes 21390fb46e92
echo $row->msisdncountrycode; //echoes DE
echo $row->statuscode; //echoes HLRSTATUS_UNDELIVERED
echo $row->hlrerrorcodeid; //echoes 9
}
Echoes are based on current input. Since its in loop, it will print all values.
You can try this. Its working fine for me.
json_decode($content)
Try this:
<?php
$content='{"success":true,"results":[{"id":"21390fb46e92","msisdncountrycode":"DE","msisdn":"+491788735000","statuscode":"HLRSTATUS_UNDELIVERED","hlrerrorcodeid":9,"subscriberstatus":"SUBSCRIBERSTATUS_ABSENT","imsi":null,"mccmnc":"26203","mcc":"262","mnc":"03","msin":null,"servingmsc":null,"servinghlr":null,"originalnetworkname":"E-Plus","originalcountryname":"Germany","originalcountrycode":"DE","originalcountryprefix":"+49","originalnetworkprefix":"178","roamingnetworkname":null,"roamingcountryname":null,"roamingcountrycode":null,"roamingcountryprefix":null,"roamingnetworkprefix":null,"portednetworkname":null,"portedcountryname":null,"portedcountrycode":null,"portedcountryprefix":null,"portednetworkprefix":null,"isvalid":"Yes","isroaming":"No","isported":"No","usercharge":"0.0100","inserttime":"2015-11-24 18:56:42.048693+08","storage":"CURL-TEST","route":"IP1"}]}';
echo '<pre>';
$jsonArr = json_decode($content, TRUE);
$outputArr = $jsonArr['results'][0];
print_r($outputArr);
$id = $outputArr['id'];
$msisdncountrycode = $outputArr['msisdncountrycode'];
$statuscode = $outputArr['statuscode'];
$hlrerrorcodeid = $outputArr['hlrerrorcodeid'];
?>

How to parse a specific json array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a php beginner. I've got this kind of JSON ?
{
"rows":
[
["/page1.php","568"],
["/","78"],["/page2.php","4"],
["/page2.php","2"],
["/page3.php","1"],
["/search.php","1"]
]
}
to parse json i use
<?php
$json_file = file_get_contents('test.json');
$parsed_json = json_decode($json_file);
?>
But i don't know to parse each page and number. Anyone can help me on this surely basic issue ?
thanks a lot
$parsed_json will be an object, whose rows property is an array. The elements of this array are arrays, whose first element is the page name, the second element is the page number:
foreach ($parsed_json->rows as $page) {
$page_name = $page[0];
$page_number = $page[1];
echo "Page #" . $page_number . " on " . $page_name . "<br>";
}

Assign text between tags to an variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Is it possible to get the text between <p></p> tags and set this in a variable?
<p>blabla</p> So i would like to get the text "blabla" and set this into a php variable so the variable would have the text value like this:.
<?$test = blabla;?>
Try:
$html = "<p>blabla</p>";
$dom = new DOMDocument;
$dom->loadXML($html);
$arr = $dom->getElementsByTagName('p');
foreach ($arr as $value) {
echo $value->nodeValue; // result => blabla
}
There are many methods which can be used based on your needs so take a look on documentation
DOMDocument
You can use this function, it is self explanatory:
function getTextBetweenTags($string, $tagname)
{
$pattern = "/<$tagname>(.*?)<\/$tagname>/";
preg_match($pattern, $string, $matches);
return $matches[1];
}
?>

Categories