开发者

Data parsing-retrieval using php

Lets say we have the following structure in a web page. How can i retrieve the following information using php? something 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 bgco开发者_如何学JAVAlor = '#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>


You can use DOMDocument for this, pretty simple:

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

foreach($TH as $Item)
{
    echo $Item->nodeValue;
}
  • Example: http://codepad.org/VFHjlCbc
  • DOMDocument: http://www.php.net/manual/en/class.domdocument.php


You can use a regular expression to match a pattern like that. The preg_match() function is a good place to start.


I hope you accept an answer this time! Just pull out the match groups 1 to 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);

Result:

    [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
        )
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜