读取XML节点中的值

时间:2011-12-24 14:24:16

标签: php xml

我的XML文档包含以下节点:

  

1个macbook air和4个ipods

我的问题是如何读取'变量'值(1和4)并用php回显它们。

1 个答案:

答案 0 :(得分:1)

您应该使用simplexml XML解析器和一些正则表达式。

<?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]);