Citizen SP
Citizen SP

Reputation: 1411

Read values within a XML node

My XML document contains the following node:

1 macbook air and 4 ipods

My question is how to read the 'variable' values (1 and 4) and echo them with php.

Upvotes: 0

Views: 55

Answers (1)

RageZ
RageZ

Reputation: 27313

you should use the simplexml XML parser and some regular expression.

<?php
$string = <<<XML
<?xml version='1.0'?>
<document>
    <description>1 macbook air and 4 ipods</description>
</document>
XML;
$xml = simplexml_load_string($string);
$description = $xml->description;
preg_match('|(\d+) macbook air and (\d+) ipods|', $description, $match);

print_r($match[1] . ' ');
print_r($match[2]);

Upvotes: 1

Related Questions