Read only Title from a page? [duplicate] - php

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 7 years ago.
I have a page called index.php which has some content on it, but the title is set from another server that generates a text file called cache_currentsongcallapi.txt
Contents of 'cache_currentsongapi.txt':
<tracks>
<radioname>Mick&apos;s Music Station</radioname>
<rank>0</rank>
<isradionomy>1</isradionomy>
<radurl>http://www.radionomy.com/mick-smusicstation</radurl>
<track>
<uniqueid>2722440231</uniqueid>
<title>Hello</title>
<artists>Adele</artists>
<starttime>2015-11-23 21:05:02.35</starttime>
<playduration>293023</playduration>
<current>1</current>
<callmeback>217256</callmeback>
</track>
</tracks>
Is there any way that I can take ONLY the title from the text file and display it in the index page?
NOTE:
This method cannot include editing the text file as it is overwritten by the server.
The website that this code will go on: http://mickyd.net/radio

This appears like valid XML. Never, never read XML using regular expressions. You'll hear this a lot, and for good reasons.
I'd start by reading the string as XML:
$xml = new SimpleXMLElement($string);
$result = $xml->xpath('/tracks/track/title');
Your mileage may vary, but basically you can do a lot starting from here. I'd suggest reading a bit more on DOMXPath and SimpleXMLElement.

Here's a full version including the reading of the actual file since you seem to need help there too, but please mark Victor's as the answer.
$filePath = "file.txt";
$handle = fopen($filePath, "r");
$fileContents = fread($handle, filesize($filePath));
$xml = new SimpleXMLElement($fileContents);
$title = $xml->xpath("/tracks/track/title");
echo($title[0]);
Where "file.txt" is the path to the file.

Related

Remove XML Node from a file loaded using PHP filesystem [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 4 years ago.
I have an XML file that's read using PHP's file_get_contents so other changes can be done to it.
I need to find and remove the nodes <BATCHALLOCATIONS.LIST>...<BATCHALLOCATIONS.LIST> (not just those two lines, but what's between the entire node) in the entire file.
Since the file is already loaded using file_get_contents I'd like to do this without having to load the file again using simpleXML, or an XML parser or any other method (like DOM).
The node does not have a specific parent and appears randomly.
The XML file is exported from a Business Accounting Software.
Any idea on how to achieve this? Maybe using a Regular Expression to do a search and replace or something like that?
I've been trying to do this using a regular expression and preg_replace, but just can't get things to work.
Here's just a portion of the file. The original runs to 10K+ lines.
This should have worked but doesn't
preg_replace('/^\<BATCHALLOCATIONS.LIST\>(.*?)\<\BATCHALLOCATIONS.LIST\>$/ism','', $newXML);
I'm trying to do this without using any HTML/XML parser.
There's probably a better way to do it, but this will work
// get your file as a string
$yourXML = file_get_contents($file) ;
$posStart = stripos($yourXML,'<BATCHALLOCATIONS.LIST>') ;
$posEnd = stripos($yourXML,'</BATCHALLOCATIONS.LIST>') + strlen('</BATCHALLOCATIONS.LIST>') ;
$newXML = substr($yourXML,0,$posStart) . substr($yourXML,$posEnd) ;

Getting Specific Tag from Remote Site using PHP [duplicate]

This question already has answers here:
how to use dom php parser
(4 answers)
Closed 9 years ago.
<?php
$html = file_get_contents('http://xpool.xram.co/index.cgi');
echo $html;
?>
I want to get information in a tag on a remote web site using php. and only the tags.
I found this small string that is great for retrieving the entire site source. However, i want to get a small section only. How can I filter out all the other tags and get only the one tag I need?
I'd suggest using a PHP DOM parser. (http://simplehtmldom.sourceforge.net/manual.htm)
require_once ('simple_html_dom.php');
$html = file_get_contents('http://xpool.xram.co/index.cgi');
$p = $html->find('p'); // Find all p tags.
$specific_class = $html->find('.classname'); // Find elements with classname as class.
$element_id = $html->find('#element'); // Find element with the id element
Read the docs, there are tons of other options available.

Extracting XML data to php [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I'm trying to extract data from XML file (http://freegeoip.net/xml/google.com). You can see the content of the file looks something like:
<Response>
<Ip>74.125.235.3</Ip>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>CA</RegionCode>
<RegionName>California</RegionName>
<City>Mountain View</City>
<ZipCode>94043</ZipCode>
<Latitude>37.4192</Latitude>
<Longitude>-122.0574</Longitude>
<MetroCode>807</MetroCode>
<AreaCode>650</AreaCode>
</Response>
I want to take the information stored in the <latitude> and <longitude> tags, and store them in separate variables. The problem is, I've little idea how to do this, and was wondering if anyone could show me how to parse XML files with php?
$string_data = "<your xml response>";
$xml = simplexml_load_string($string_data);
$latitude = (string) $xml->Latitude;
$longitude = (string) $xml->Longitude;
echo $latitude.' '.$longitude;
It's easy, use PHP's SimpleXML Library:
$xml = simplexml_load_file("http://freegeoip.net/xml/google.com");
echo $xml->Ip; // 173.194.38.174
echo $xml->CountryCode; // US
echo $xml->ZipCode; // 94043
// etc...
The PHP manual has a whole section on PHP parsing:
http://php.net/manual/en/book.simplexml.php
http://php.net/manual/en/book.xml.php
For simplicity, you could also use xml_parse_into_struct()
Here's a pretty good example, using SimpleXML:
http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

Print out GData title in PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have this link
http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1
That gives me the latest video from GudjoDaneel but I'd like to print our this title inside a PHP file
<title type='text'>The GD Project S3 | NEVER GIVE UP! | Division 1</title><content type='text'>
I'd appreciate it if someone could help me where to begin. And what I could look up.
I'd suggest looking up SimpleXML. It's easy to use once you get the hang of it, and you can get the title in just four line:
$url = 'http://gdata.youtube.com/feeds/api/users/gudjondaniel/uploads?max-results=1';
$source = file_get_contents($url);
$xml = new SimpleXMLElement($source);
$title = $xml->entry->title;
Do note, though, that $title is a PHP object in this case. If you echo it straight away, it'll be reinterpreted as a string, and everything will be alright. If you plan on doing anything else with it, you'll need to cast it as a string, like this:
$title = strval($title);

Load and parse XML file with PHP [duplicate]

This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 9 years ago.
I have to create a webservice which goes to a specific URL that returns a XML-file as response and interprets/parses this file in order to save its contents to a MySQL database.
I've heard about the SimpleXML but I'm not sure how to get the websites response into a file whose path is needed in order to parse the document.
Can somebody at least explain me how to reach the goal of downloading the XML and saving it to a file? (best with some PHP code)
I will then (hopefully) find out by myself how to parse it and store its contents.
Here's an example of what my XML will look like (for privacy reasons I can't publish the real URL I'm using...)
Here's a couple of pointers..
To download a file and save it, the easiest way I have found is this:
<?php
file_put_contents('saved.xml', file_get_contents('http://www.xmlfiles.com/examples/simple.xml'));
You can then open the file with the simpleXML library like so:
$xml = simplexml_load_file('saved.xml');
var_dump($xml);
Hope that gives you enough info to get started.
See simpleXML for info on the simpleXML library.
You can download and save the xml to a local file by doing this:
$xmlstring = file_get_contents("http://domain.com/webservice/xmlfile.xml");
file_put_contents("path/localxmlfile.xml", $xmlstring);
To parse the xml file I suggest you to use DOMDocument class in combination with the DOMXPath class to query/search for specific elements.
DOMDocument: http://php.net/manual/de/class.domdocument.php
DOMXPath: http://php.net/manual/de/class.domxpath.php
Hopefully you can find your answer on below link. Seems related topic.
How do you parse and process HTML/XML in PHP?

Categories