我在大学时都是PHP正则表达式的新手,但我有点想到我需要做的事情。基本上我需要创建一个PHP程序来读取包含几个故事的XML源代码。并将其详细信息存储在mySQL数据库中。我设法创建了一个选择每个故事的表达式,但我需要进一步打破这个表达式,以便在故事中获得每个元素。这是XML:
XML
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<latestIssue>
<issue number="256" />
<date>
<day> 21 </day>
<month> 1 </month>
<year> 2011 </year>
</date>
<story>
<title> Is the earth flat? </title>
<author> A. N. Redneck </author>
<url> http://www.HotStuff.ie/stories/story123456.xml </url>
</story>
<story>
<title> What the actress said to the bishop </title>
<author> Brated Film Critic </author>
<url> http://www.HotStuff.ie/stories/story123457.xml </url>
</story>
<story>
<title> What the year has in store </title>
<author> Stargazer </author>
<url> http://www.HotStuff.ie/stories/story123458.xml </url>
</story>
</latestIssue>
所以我需要从每个故事中获取标题,作者和网址,并将它们作为一行添加到我的数据库中。这就是我到目前为止所拥有的:
PHP
<?php
$url = fopen("http://address/to/test.xml", "r");
$contents = fread($url,10000000);
$exp = preg_match_all("/<title>(.+?)<\/url>/s", $contents, $matches);
foreach($matches[1] as $match) {
// NO IDEA WHAT TO DO FROM HERE
// $exp2 = "/<title>(.+?)<\/title><author>(.+?)<\/author><url>(.+?)<\/url>/";
// This is what I had but I'm not sure if it's right or what to do after
}
?>
我非常感谢那些帮助过的人,我整天都被困在这里,而且根本无法解决正则表达式问题。一旦我设法获得每个故事的详细信息,我就可以轻松更新数据库。
修改 感谢您的回复,但您确定这不能用正则表达式完成吗?只是问题说&#34;使用正则表达式来分析XML并提取您需要的相关数据。请注意,有关每个故事的信息分布在几行XML和#34;中。也许他犯了一个错误,但我不知道为什么他会这样写,如果不能这样做的话。
答案 0 :(得分:0)
答案 1 :(得分:0)
正则表达式不是此处使用的正确工具。您想要使用XML解析器。我喜欢PHP的SimpleXML
$sXML = new SimpleXMLElement('http://address/to/test.xml', 0, TRUE);
$stories = $sXML->story;
foreach($stories as $story){
$title = (string)$story->title;
$author = (string)$story->author;
$url = (string)$story->url;
}
答案 2 :(得分:0)
你永远不应该使用regexp来解析XML文档(好吧,从来不是一个大词,在极少数情况下,正则表达式可以更好但不是你的情况)。
由于这是一个文档阅读,我建议您使用SimpleXML类和XPath查询。 例如:
$ cat test.php
#!/usr/bin/php
<?php
function xpathValueToString(SimpleXMLElement $xml, $xpath){
$arrayXpath = $xml->xpath($xpath);
return ($arrayXpath) ? trim((string) $arrayXpath[0]) : null;
}
$xml = new SimpleXMLElement(file_get_contents("test.xml"));
$arrayXpathStories = $xml->xpath("/latestIssue/story");
foreach ($arrayXpathStories as $story){
echo "Title : " . xpathValueToString($story, 'title') . "\n";
echo "Author : " . xpathValueToString($story, 'author') . "\n";
echo "URL : " . xpathValueToString($story, 'url') . "\n\n";
}
?>
$ ./test.php
Title : Is the earth flat?
Author : A. N. Redneck
URL : http://www.HotStuff.ie/stories/story123456.xml
Title : What the actress said to the bishop
Author : Brated Film Critic
URL : http://www.HotStuff.ie/stories/story123457.xml
Title : What the year has in store
Author : Stargazer
URL : http://www.HotStuff.ie/stories/story123458.xml