-2

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

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • This txt file can actually be treated and parsed as an XML file. Have you attempted to read the file yet ? – Ohgodwhy Nov 23 '15 at 20:18
  • sorry, chris85, but I have tried that and simply do not get it! Can you explain it to me in simple reference? – Michael Davison Nov 23 '15 at 20:28
  • You have tried what exactly? There are 20 different solutions in that dupe. None of them worked for you? – PeeHaa Nov 23 '15 at 20:31
  • You have an answer below that should give you a starting point. There are links here and in that thread that should be able to assist you further. If you have a particular issue please post on that topic. Please also use the `@` to tag users, notifications aren't sent otherwise. – chris85 Nov 23 '15 at 20:36

2 Answers2

1

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.

Victor Nițu
  • 1,485
  • 13
  • 18
0

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.

Chris Evans
  • 1,235
  • 10
  • 14