读取XML节点中的值

时间:2021-07-25 17:02:52

My XML document contains the following node:

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

1 macbook air and 4 ipods

1个macbook air和4个ipods

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

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

1 个解决方案

#1


1  

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

你应该使用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]);

#1


1  

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

你应该使用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]);