我想把xml(网站)加工成php数组。
我已经尝试了以下代码,该代码适用于结果中的每一个但我需要得到总页数,我无法看到我如何能够做到这一点。
function get_content($url)
/// basically opens the page and stores it as a variable. Buggered if I know how it works!
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec ($ch);
curl_close ($ch);
$string = ob_get_contents();
ob_end_clean();
return $string;
$string = NULL;
$ch = NULL;
$url = NULL;
}
$url = "url";
$content = get_content($url);
$content_x = explode("<result>", $content);
foreach ($content_x as $item)
{
$p1 = strpos($item, '<title>');
$p2 = strpos($item, '</title>');
$l1 = $p2 - $p1;
echo '<br>'.$title = substr($item, $p1, $l1);
}
xml网站Feed
<?xml version="1.0" encoding="UTF-8"?>
<response version="2">
<totalpage>1005</totalpage>
<results>
<result>
<title>test</title>
<title2>test2</title2>
<title3>test3</title3>
<result>
<result>
<title>test</title>
<title2>test2</title2>
<title3>test3</title3>
<result>
<result>
<title>test</title>
<title2>test2</title2>
<title3>test3</title3>
<result>
........so on
<results>
</response>
我需要总页数并且每次都在结果中
如何获取总页数,这是处理结果的更好方法
答案 0 :(得分:0)
你绝对不应该使用字符串操作来尝试解析XML。有许多PHP库可以做到这一点。我可能会推荐SimpleXML。
用法是:
$xml = simplexml_load_string($content);
$totalpage = $xml->response->totalpage;