如何编写php让我知道这里的链接? 总是会有不同的环节如何将XML转换为PHP
<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>
如何编写php让我知道这里的链接? 总是会有不同的环节如何将XML转换为PHP
<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>
$xml ='<response>
<redirect>
http://www.example.com/
</redirect>
<code>0</code>
<description>OK</description>
</response>';
$xml = simplexml_load_string($xml);
echo $xml->redirect; // http://www.example.com/
打我吧:) – JustinM151
需要快速:) –
simplexml_load_string
这应该回答你所有的问题...
http://www.php.net/manual/en/function.simplexml-load-string.php
使用DOM + X路径:
$xml = <<<'XML'
<response>
<redirect>http://www.example.com/</redirect>
<code>0</code>
<description>OK</description>
</response>
XML;
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
var_dump($xpath->evaluate('string(/response/redirect)'));
输出:
string(23) "http://www.example.com/"
如果你的XML数据确实是这个简单的正则表达式可以做的工作。如果不这样做,[XMLReader](http://www.php.net/manual/en/book.xmlreader.php)将做到这一点。 –