REGEX PHP字符串到JSON输出

时间:2013-09-22 21:05:25

标签: php html regex json

我有一个网站的源代码(从file_get_contents获取)。

看起来如此:

<td id=dat>
    2013-09-22
</td>
<td>
    <a href="http://www.test.at/results/show_results.php?v=12252" 
        target=_blank>bbbbbbb
    </a>
</td>
<td id=dat>
    2013-09-22
</td>
<td>
    <a href="http://www.test.at/results/show_results.php?v=12231" 
        target=_blank>Tlalalala
    </a>
</td>
<td id=dat>
    2013-09-22
</td>
<td>
    <a href="http://www.test.at/results/show_results.php?v=12234"
        target=_blank>Tzzzzz
    </a>
</td>

现在我需要做一个正则表达式,这样我就可以拥有它......

<?php

$JSON = array(
    "results" => array(
        array(
            "title" => "bbbbbbb",
            "url" => "http://www.test.at/results/show_results.php?v=12252",
            "date" => "2013-09-22"
        ),
        array(
            "title" => "Tlalalala",
            "url" => "http://www.test.at/results/show_results.php?v=12231",
            "date" => "2013-09-22"
        ),
        array(
            "title" => "Tzzzzz",
            "url" => "http://www.test.at/results/show_results.php?v=12234",
            "date" => "2013-09-22"
        ),
    ),
);

echo json_encode($JSON);

?>

有人能帮助我吗?

1 个答案:

答案 0 :(得分:2)

就我个人而言,我建议使用html Dom解析器并以这种方式进行 - 看看这个有一些例子,你可以看看 http://simplehtmldom.sourceforge.net/