使用php进行数据解析检索

时间:2011-02-20 19:17:30

标签: php information-retrieval data-retrieval

假设我们在网页中有以下结构。如何使用PHP检索以下信息? 某物       XAXAXA-SASASASA 2-1       ZAZAZAZA-CACACACA 2-2

<th class='black' colspan='6'>something</th>
<tr class=''>
        <td bgcolor = '#272727' width = 40%>XAXAXA</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '40%'>SASASASA</td>
        <td bgcolor = '#272727'  width = '5%'>2</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '5%'>1</td>
    </tr>
<tr class=''>
        <td bgcolor = '#484848' width = 40%>ZAZAZAZA</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '40%'>CACACACA</td>
        <td bgcolor = '#484848'  width = '5%'>2</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '5%'>2</td>
    </tr>

3 个答案:

答案 0 :(得分:1)

您可以使用DOMDocument,非常简单:

$DOM = new DOMDocument();
$DOM->loadHTML($Content);
$TH = $DOM->getElementsByTagName("td");

foreach($TH as $Item)
{
    echo $Item->nodeValue;
}

答案 1 :(得分:-1)

您可以使用正则表达式来匹配这样的模式。 preg_match()函数是一个很好的起点。

答案 2 :(得分:-1)

我希望你这次接受答案!只需拉出比赛组1到13。

$sourcestring="your source string";
preg_match_all('/<th[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*).*?<td[^>]*>([^<]*)/is',$sourcestring,$matches);
echo "<pre>".print_r($matches,true);

结果:

    [0] => Array
        (
            [0] => <th class='black' colspan='6'>something</th>
<tr class=''>
        <td bgcolor = '#272727' width = 40%>XAXAXA</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '40%'>SASASASA</td>
        <td bgcolor = '#272727'  width = '5%'>2</td>
        <td bgcolor = '#272727'  width = '5%'> - </td>
        <td bgcolor = '#272727' width = '5%'>1</td>
    </tr>
<tr class=''>
        <td bgcolor = '#484848' width = 40%>ZAZAZAZA</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '40%'>CACACACA</td>
        <td bgcolor = '#484848'  width = '5%'>2</td>
        <td bgcolor = '#484848'  width = '5%'> - </td>
        <td bgcolor = '#484848' width = '5%'>2
        )

    [1] => Array
        (
            [0] => something
        )

    [2] => Array
        (
            [0] => XAXAXA
        )

    [3] => Array
        (
            [0] =>  - 
        )

    [4] => Array
        (
            [0] => SASASASA
        )

    [5] => Array
        (
            [0] => 2
        )

    [6] => Array
        (
            [0] =>  - 
        )

    [7] => Array
        (
            [0] => 1
        )

    [8] => Array
        (
            [0] => ZAZAZAZA
        )

    [9] => Array
        (
            [0] =>  - 
        )

    [10] => Array
        (
            [0] => CACACACA
        )

    [11] => Array
        (
            [0] => 2
        )

    [12] => Array
        (
            [0] =>  - 
        )

    [13] => Array
        (
            [0] => 2
        )