How to read in XML strings as variables? [duplicate] - php

This question already has answers here:
A simple program to CRUD node and node values of xml file [closed]
(2 answers)
Closed 8 years ago.
Try this query:
http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da
How do I simply read in the suggestions as variables? I can't seem to find anything that explains this.

You can use SimpleXmlIterator. That's really easy to use and you will be able to perform a foreach on the object you will get.
Library source
For example with file_get_contents or replace with curl if you prefer:
$feed = new SimpleXmlIterator(file_get_contents('http://www.dictionaryapi.com/api/v1/references/collegiate/xml/gobabola?key=135a6187-af83-4e85-85c1-1a28db11d5da'));
foreach ($feed->suggestion as $suggestion) {
echo $value;
}

Related

Getting second last parameter from querystring with PHP [duplicate]

This question already has answers here:
Getting parts of a URL (Regex)
(30 answers)
Closed 3 years ago.
I have a URL like so:
http://example.com/var1/var2
What I'd like to is get VAR 1 from the querystring. I've tried using a regex but I'm not really very familiar with them. Is there an easier way?
Another approach
<?php
$e = explode("/",parse_url("http://example.com/a/b")["path"])[1];
// $e now is "a"

unserialize.com how they do it [duplicate]

This question already has answers here:
What kind of string is this? How do I unserialize this string? [duplicate]
(2 answers)
Closed 6 years ago.
I got the following string:
a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}
I can't use
var_dump(json_decode($json))
because it doesn't return anything. I found 2 websites that where a bit usefull:
http://jsonlint.com/ for checking if the object is valid (what it ain't) and http://www.unserialize.com/ wich can 'unserialize' the json string back to an array.
Now I wonder what unserialize does to the json string. So I can use it in my script as well.
Looks like this is in serialized form. You need to use unserialize() function here.
$input = 'a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}';
var_dump(unserialize($input));
Use unserialize function in php
<?php
echo '<pre>';
print_r(unserialize('a:16:{i:0;s:1:"6";i:1;s:2:"12";i:2;s:1:"2";i:3;s:2:"11";i:4;s:1:"7";i:5;s:2:"10";i:6;s:2:"16";i:7;s:1:"5";i:8;s:1:"3";i:9;s:1:"1";i:10;s:2:"14";i:11;s:1:"8";i:12;s:2:"13";i:13;s:1:"9";i:14;s:1:"4";i:15;s:2:"15";}'));
?>
https://eval.in/637790

PHP getting specific tags from file_get_contents [duplicate]

This question already has answers here:
How to get content from specific tags when using file_get_contents
(2 answers)
Closed 8 years ago.
$landings = file_get_contents('http://www.domain.co.uk/page.php');
In the above example URL I want to only select and copy the HTML from a specific element #sidebar-bgbtm
From there I would like to export the HTML as a JSON value.
Is there any way of doing that?
Thanks
If you are familiar with jQuery syntax, PHP Simple HTML DOM might be good place to start
http://simplehtmldom.sourceforge.net/
include('simple_html_dom.php');
$html = file_get_html('http://www.domain.co.uk/page.php');
$result = $html->find('#sidebar-bgbtm', 0)->outertext;
And then export to json:
echo json_encode( array( 'outertext' => $result) );

Get DOM element attribute, using php [duplicate]

This question already has answers here:
PHP: DomElement->getAttribute
(4 answers)
Closed 9 years ago.
If I have a PHP variable that is a string such as this:
$link = "<a href='test.jpg'>Download</a>";
How can I use the variable $link to get the value for href using PHP and not javascript?
If the text big, you can use some library as phpQuery, Zend DOM Query, Nokogiri or other. Otherwise, use regexp.

Tags still remaining after using simplexml_load_string [duplicate]

This question already has answers here:
Getting actual value from PHP SimpleXML node [duplicate]
(4 answers)
Closed 8 years ago.
I am using simplexml_load_string for XML packets. In my scenario, the XML string I want to convert is known as k.
My problem, however, is that when I use k, tags still remain that weren't parsed (<k>, <\k>).
For example, I use
$x->k, and I get back <k>DATA I WANT HERE<\EK>.
How do I get rid of these?
What the code does: It connects to a game and logs in.
Use InnerNode to get the value without the tags:
$x->k->InnerNode
You can also do a typecast:
(string)$x->k
I tried this and seem to be getting the string.
<?php
$str = "<msg t='sys'><body action='rndK' r='-1'><k>qH~e9Gmt</k></body></msg>";
$xml = simplexml_load_string( $str );
echo $xml->body->k; // gives 'qH~e9Gmt'
?>

Categories