如何在多个<title>标签</title>之间获取所有内容

时间:2013-09-04 23:14:35

标签: php xml

我想在这个网站的标题标签之间获得多个标题:

http://drewgarcia23.3owl.com/LiveScores/mlb.php

我尝试过simplexml方式。但这可能是愚蠢的,因为它是一个php文件。任何帮助将不胜感激。我也尝试过preg匹配标签并剥离标签并获取错误。感到沮丧,所以我发布这个。 编码不好

$url = 'http://drewgarcia23.3owl.com/LiveScores/mlb.php';
$rss = simplexml_load_file($url);
$items = $rss->channel->item;
echo $items;

2 个答案:

答案 0 :(得分:1)

simplexml_load_file()函数将XML文件解释为对象。

这是一个生成xml的php文件。所以:

$url = 'http://drewgarcia23.3owl.com/LiveScores/mlb.php';
$items = simplexml_load_file($url);

foreach ($items->channel->item as $item) {
    echo $item->title . "<br>";
}

标题输出成功。

NY Mets 5 Atlanta 2 (FINAL)
Minnesota 5 Houston 6 (FINAL)
Miami 7 Chicago Cubs 9 (FINAL)
...

答案 1 :(得分:0)

试试这个正则表达式:

/<title>(.+)<\/title>/i

preg_match_all('/<title>(.+)<\/title>/i', '{{your data}}', $arr, PREG_PATTERN_ORDER);

Array
(
    [0] => Array
        (
            [0] => <title>MLB Scores</title>
            [1] => <title>MLB Scores</title>
            [2] => <title>NY Mets 5 Atlanta 2 (FINAL)</title>
            [3] => <title>Minnesota 5 Houston 6 (FINAL)</title>
            [4] => <title>Miami 7 Chicago Cubs 9 (FINAL)</title>
            [5] => <title>Texas 4 Oakland 11 (FINAL)</title>
            [6] => <title>Toronto 3 Arizona 4 (FINAL - 10 INNINGS)</title>
            [7] => <title>San Francisco 0 San Diego 2 (BOT 2ND)</title>
            [8] => <title>Detroit 0 Boston 0 (TOP 1ST)</title>
            [9] => <title>Baltimore 0 Cleveland 0 (BOT 1ST)</title>
            [10] => <title>Chicago Sox 1 NY Yankees 0 (TOP 1ST)</title>
            [11] => <title>Washington 1 Philadelphia 0 (TOP 1ST)</title>
            [12] => <title>St. Louis 0 Cincinnati 0 (BOT 1ST)</title>
            [13] => <title>Seattle at Kansas City (8:10 PM ET)</title>
            [14] => <title>Pittsburgh at Milwaukee (8:10 PM ET)</title>
            [15] => <title>LA Dodgers at Colorado (8:40 PM ET)</title>
            [16] => <title>Tampa Bay at LA Angels (10:00 PM ET)</title>
        )

    [1] => Array
        (
            [0] => MLB Scores
            [1] => MLB Scores
            [2] => NY Mets 5 Atlanta 2 (FINAL)
            [3] => Minnesota 5 Houston 6 (FINAL)
            [4] => Miami 7 Chicago Cubs 9 (FINAL)
            [5] => Texas 4 Oakland 11 (FINAL)
            [6] => Toronto 3 Arizona 4 (FINAL - 10 INNINGS)
            [7] => San Francisco 0 San Diego 2 (BOT 2ND)
            [8] => Detroit 0 Boston 0 (TOP 1ST)
            [9] => Baltimore 0 Cleveland 0 (BOT 1ST)
            [10] => Chicago Sox 1 NY Yankees 0 (TOP 1ST)
            [11] => Washington 1 Philadelphia 0 (TOP 1ST)
            [12] => St. Louis 0 Cincinnati 0 (BOT 1ST)
            [13] => Seattle at Kansas City (8:10 PM ET)
            [14] => Pittsburgh at Milwaukee (8:10 PM ET)
            [15] => LA Dodgers at Colorado (8:40 PM ET)
            [16] => Tampa Bay at LA Angels (10:00 PM ET)
        )

)