使用php进行数据解析检索

发布于 2024-10-18 02:10:49 字数 1043 浏览 5 评论 0原文

假设我们的网页有以下结构。如何使用 php 检索以下信息? 某物 XAXAXA-莎莎莎 2-1 扎扎扎扎-卡卡卡卡 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>

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 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2024-10-25 02:10:49

您可以使用 DOMDocument 来实现此目的,非常简单:

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

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

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;
}
白龙吟 2024-10-25 02:10:49

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

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

九公里浅绿 2024-10-25 02:10:49

希望这次您能接受答案!只需拉出第 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
        )

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
        )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文