【发布时间】:2012-02-08 17:35:08
【问题描述】:
我正在阅读一个 RSS 提要。我需要从该提要的字段中检索某些数据。
这是示例提要数据:
<content:encoded><![CDATA[
<b>When:</b><br />
Weekly Event - Every Thursday: 1:30 PM to 3:30 PM (CT)<br /><br />
<b>Where:</b><br />
100 West Street<BR>2nd floor<BR>Gainesville<BR>
<br>.....
如何分别提取时间:和地点:的数据?我尝试使用正则表达式,但我不确定我是否没有正确访问数据或者我的正则表达式是否错误。我不打算使用正则表达式。
这是我的代码:
foreach ($x->channel->item as $event) {
$eventCounter++;
$rowColor = ($eventCounter % 2 == 0) ? '#FFFFFF' : '#F1F1F1';
$content = $event->children('http://purl.org/rss/1.0/modules/content/');
$contents = $content->encoded;
echo '<tr style="background-color:' . $rowColor . '">';
echo '<td>';
//echo "<a id=buttonRed href='$event->link' title='$event->title' target='_blank'>" . $event->title . "</a>";
echo "" . $event->title . "";
echo '</td>';
echo '<td>';
$re = '%when\:\s*</b>\s*(.|\s)<br \/><br \/>$/i';
if (preg_match($re, $contents, $matches)) {
$date = $matches;
}
echo $date;
echo '</td>';
echo '<td>';
$re = '/^When\:<\/b>()$/';
if (preg_match($re, $contents, $matches)) {
$location = $matches;
}
echo $location;
echo '</td>';
echo '<td>';
echo "<a id=buttonRed href='$event->link' title='$event->title' target='_blank'>Click Here To Register</a>";
echo '</td>';
echo '</tr>';
}
这两个 $res 只是我尝试使用不同的正则表达式获取数据。让我知道我哪里出错了。谢谢
【问题讨论】:
-
不要对他投反对票。他说他不喜欢正则表达式方法。让他看看你是怎么做的。
-
谢谢,正如乔纳森所说。我并不是说我必须使用正则表达式。我也先尝试了一些 strpos,但加载不正确。我只是想知道如何获取那些特定的数据字段。
-
@JonathanM:他之前被告知过。 stackoverflow.com/questions/7969090/…