php从html页面中提取信息

发布于 2024-10-20 09:07:38 字数 3510 浏览 8 评论 0原文

我有这段代码

<input type=hidden name="code1" value="AA-T5301">
    <tr>
        <td align=left valign=middle class="stdtext">
            AA-T5301
        </a>
        </td>
        <td valign=middle align=left class="stdtext">
            <a onMouseOver="window.status='See the more info on '; return true"
                HREF="product.asp?ms=&dept_id=322&sku=32&nav=">
                Grapeseed Oil 150ml
            </A>
        </td>
        <td valign=middle align=right class="stdtext">Order Now</td>
        <td valign=middle align=right class="stdtext">
            <font class="productsale">
                <strike>£3.04</strike>
                &#160;
            </font>
            £2.04
        </td>
        <td valign=middle align=right class="stdtext">
            <input type=text size=4 name="qty_AA-T5301" value="0">
        </td>
    </tr>
    <input type=hidden name="code2" value="AA-T5302">
        <tr>
            <td align=left valign=middle class="stdtext">
                AA-T5302
            </a>
            </td>
            <td valign=middle align=left class="stdtext">
                <a onMouseOver="window.status='See the more info on '; return true"
                    HREF="product.asp?ms=&dept_id=322&sku=143&nav=">
                    Grapeseed Oil 500ml
                </A>
            </td>
            <td valign=middle align=right class="stdtext">Order Now</td>
            <td valign=middle align=right class="stdtext">
                <font class="productsale">
                    <strike>£6.46</strike>
                    &#160;
                </font>
                £4.33
            </td>
            <td valign=middle align=right class="stdtext">
                <input type=text size=4 name="qty_AA-T5302" value="0">
            </td>
        </tr>
        <input type=hidden name="code3" value="AA-T530">
            <tr>
                <td align=left valign=middle class="stdtext">
                    AA-T530
                </a>
                </td>
                <td valign=middle align=left class="stdtext">
                    <a onMouseOver="window.status='See the more info on '; return true"
                        HREF="product.asp?ms=&dept_id=322&sku=19&nav=">
                        Grapeseed Oil 50ml
                    </A>
                </td>
                <td valign=middle align=right class="stdtext">Out of Stock</td>
                <td valign=middle align=right class="stdtext">
                    <font class="productsale">
                        <strike>£1.75</strike>
                        &#160;
                    </font>
                    £1.17
                </td>
                <td valign=middle align=right class="stdtext">
                    <input type=text size=4 name="qty_AA-T530" value="0">
                </td>
            </tr>

我怎样才能将信息提取到一个数组中,所以我有这样的东西..

product_code_array=(AA-T5301,AA-T5302,AA-T530);

RRP_array=(3.04,6.46,1.75);

price_array=(2.04,4.33,1.17);

注意:页面上一次可能有超过 3 个项目,或者可能只有 1 个

I have this code

<input type=hidden name="code1" value="AA-T5301">
    <tr>
        <td align=left valign=middle class="stdtext">
            AA-T5301
        </a>
        </td>
        <td valign=middle align=left class="stdtext">
            <a onMouseOver="window.status='See the more info on '; return true"
                HREF="product.asp?ms=&dept_id=322&sku=32&nav=">
                Grapeseed Oil 150ml
            </A>
        </td>
        <td valign=middle align=right class="stdtext">Order Now</td>
        <td valign=middle align=right class="stdtext">
            <font class="productsale">
                <strike>£3.04</strike>
                 
            </font>
            £2.04
        </td>
        <td valign=middle align=right class="stdtext">
            <input type=text size=4 name="qty_AA-T5301" value="0">
        </td>
    </tr>
    <input type=hidden name="code2" value="AA-T5302">
        <tr>
            <td align=left valign=middle class="stdtext">
                AA-T5302
            </a>
            </td>
            <td valign=middle align=left class="stdtext">
                <a onMouseOver="window.status='See the more info on '; return true"
                    HREF="product.asp?ms=&dept_id=322&sku=143&nav=">
                    Grapeseed Oil 500ml
                </A>
            </td>
            <td valign=middle align=right class="stdtext">Order Now</td>
            <td valign=middle align=right class="stdtext">
                <font class="productsale">
                    <strike>£6.46</strike>
                     
                </font>
                £4.33
            </td>
            <td valign=middle align=right class="stdtext">
                <input type=text size=4 name="qty_AA-T5302" value="0">
            </td>
        </tr>
        <input type=hidden name="code3" value="AA-T530">
            <tr>
                <td align=left valign=middle class="stdtext">
                    AA-T530
                </a>
                </td>
                <td valign=middle align=left class="stdtext">
                    <a onMouseOver="window.status='See the more info on '; return true"
                        HREF="product.asp?ms=&dept_id=322&sku=19&nav=">
                        Grapeseed Oil 50ml
                    </A>
                </td>
                <td valign=middle align=right class="stdtext">Out of Stock</td>
                <td valign=middle align=right class="stdtext">
                    <font class="productsale">
                        <strike>£1.75</strike>
                         
                    </font>
                    £1.17
                </td>
                <td valign=middle align=right class="stdtext">
                    <input type=text size=4 name="qty_AA-T530" value="0">
                </td>
            </tr>

How can i extract the info into an array so i have something like this..

product_code_array=(AA-T5301,AA-T5302,AA-T530);

RRP_array=(3.04,6.46,1.75);

price_array=(2.04,4.33,1.17);

Note: There maybe more than 3 items on a page at a time or there may only be 1

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

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

发布评论

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

评论(3

硬不硬你别怂 2024-10-27 09:07:38

您可以尝试使用 DOMDocument 类,但您必须修复您的 html。您有结束链接标记 (),但没有任何开始链接标记 ( )

You can try and use the DOMDocument class, but you have to fix your html. You have end link tags (</a>) without any start link tags (<a href=""> )

扎心 2024-10-27 09:07:38
<?php
 $text = '<input type=hidden name="code1" value="AA-T5301">
    <tr>
        <td align=left valign=middle class="stdtext">
            AA-T5301
        </a>
        </td>
        <td valign=middle align=left class="stdtext">
            <a onMouseOver="window.status=\'See the more info on \'; return true"
                HREF="product.asp?ms=&dept_id=322&sku=32&nav=">
                Grapeseed Oil 150ml
            </A>
        </td>
        <td valign=middle align=right class="stdtext">Order Now</td>
        <td valign=middle align=right class="stdtext">
            <font class="productsale">
                <strike>£3.04</strike>
                 
            </font>
            <span id="now">£2.04</span>
        </td>
        <td valign=middle align=right class="stdtext">
            <input type=text size=4 name="qty_AA-T5301" value="0">
        </td>
    </tr>
    <input type=hidden name="code2" value="AA-T5302">
        <tr>
            <td align=left valign=middle class="stdtext">
                AA-T5302
            </a>
            </td>
            <td valign=middle align=left class="stdtext">
                <a onMouseOver="window.status=\'See the more info on \'; return true"
                    HREF="product.asp?ms=&dept_id=322&sku=143&nav=">
                    Grapeseed Oil 500ml
                </A>
            </td>
            <td valign=middle align=right class="stdtext">Order Now</td>
            <td valign=middle align=right class="stdtext">
                <font class="productsale">
                    <strike>£6.46</strike>
                     
                </font>
                <span id="now">£4.33</span>
            </td>
            <td valign=middle align=right class="stdtext">
                <input type=text size=4 name="qty_AA-T5302" value="0">
            </td>
        </tr>
        <input type=hidden name="code3" value="AA-T530">
            <tr>
                <td align=left valign=middle class="stdtext">
                    AA-T530
                </a>
                </td>
                <td valign=middle align=left class="stdtext">
                    <a onMouseOver="window.status=\'See the more info on \'; return true"
                        HREF="product.asp?ms=&dept_id=322&sku=19&nav=">
                        Grapeseed Oil 50ml
                    </A>
                </td>
                <td valign=middle align=right class="stdtext">Out of Stock</td>
                <td valign=middle align=right class="stdtext">
                    <font class="productsale">
                        <strike>£1.75</strike>
                         
                    </font>
                    <span id="now">£1.17</span>
                </td>
                <td valign=middle align=right class="stdtext">
                    <input type=text size=4 name="qty_AA-T530" value="0">
                </td>
            </tr>';

    $values = array();
    preg_match_all("#\<input type\=hidden name\=\"code[0-9]\" value\=\"(.*)\"\>#isU", $text, $values[0]);
    preg_match_all("#\<strike\>£([0-9\.]+)\<\/strike\>#isU" ,$text, $values[1]);
    preg_match_all("#\<span id\=\"now\"\>£([0-9\.]+)\<\/span\>#isU" ,$text, $values[2]);

    $product_code_array = $values[0][1];
    $RRP_array = $values[1][1];
    $price_array = $values[2][1];
?>
<?php
 $text = '<input type=hidden name="code1" value="AA-T5301">
    <tr>
        <td align=left valign=middle class="stdtext">
            AA-T5301
        </a>
        </td>
        <td valign=middle align=left class="stdtext">
            <a onMouseOver="window.status=\'See the more info on \'; return true"
                HREF="product.asp?ms=&dept_id=322&sku=32&nav=">
                Grapeseed Oil 150ml
            </A>
        </td>
        <td valign=middle align=right class="stdtext">Order Now</td>
        <td valign=middle align=right class="stdtext">
            <font class="productsale">
                <strike>£3.04</strike>
                 
            </font>
            <span id="now">£2.04</span>
        </td>
        <td valign=middle align=right class="stdtext">
            <input type=text size=4 name="qty_AA-T5301" value="0">
        </td>
    </tr>
    <input type=hidden name="code2" value="AA-T5302">
        <tr>
            <td align=left valign=middle class="stdtext">
                AA-T5302
            </a>
            </td>
            <td valign=middle align=left class="stdtext">
                <a onMouseOver="window.status=\'See the more info on \'; return true"
                    HREF="product.asp?ms=&dept_id=322&sku=143&nav=">
                    Grapeseed Oil 500ml
                </A>
            </td>
            <td valign=middle align=right class="stdtext">Order Now</td>
            <td valign=middle align=right class="stdtext">
                <font class="productsale">
                    <strike>£6.46</strike>
                     
                </font>
                <span id="now">£4.33</span>
            </td>
            <td valign=middle align=right class="stdtext">
                <input type=text size=4 name="qty_AA-T5302" value="0">
            </td>
        </tr>
        <input type=hidden name="code3" value="AA-T530">
            <tr>
                <td align=left valign=middle class="stdtext">
                    AA-T530
                </a>
                </td>
                <td valign=middle align=left class="stdtext">
                    <a onMouseOver="window.status=\'See the more info on \'; return true"
                        HREF="product.asp?ms=&dept_id=322&sku=19&nav=">
                        Grapeseed Oil 50ml
                    </A>
                </td>
                <td valign=middle align=right class="stdtext">Out of Stock</td>
                <td valign=middle align=right class="stdtext">
                    <font class="productsale">
                        <strike>£1.75</strike>
                         
                    </font>
                    <span id="now">£1.17</span>
                </td>
                <td valign=middle align=right class="stdtext">
                    <input type=text size=4 name="qty_AA-T530" value="0">
                </td>
            </tr>';

    $values = array();
    preg_match_all("#\<input type\=hidden name\=\"code[0-9]\" value\=\"(.*)\"\>#isU", $text, $values[0]);
    preg_match_all("#\<strike\>£([0-9\.]+)\<\/strike\>#isU" ,$text, $values[1]);
    preg_match_all("#\<span id\=\"now\"\>£([0-9\.]+)\<\/span\>#isU" ,$text, $values[2]);

    $product_code_array = $values[0][1];
    $RRP_array = $values[1][1];
    $price_array = $values[2][1];
?>
橙味迷妹 2024-10-27 09:07:38

首先看一下这段代码:

$dom = new domDocument;
$dom->strictErrorChecking = false;
$dom->preserveWhiteSpace = true;        
@$dom->loadHTML($html-data);
$trs=$dom->getElementsByTagName('tr');
foreach ($trs as $tr) 
{
   // now go through those elements. lookup the PHP doc on the DOM parser
   // You can iterate through sub-elements (like the 'td') just like through the 'tr'
}

您也可以根据需要直接查找或查找。根据页面的布局,您可能想要使用“更高”的节点,以便您根据标签参数或标签位置知道自己所在的位置。

Look at this code for a start:

$dom = new domDocument;
$dom->strictErrorChecking = false;
$dom->preserveWhiteSpace = true;        
@$dom->loadHTML($html-data);
$trs=$dom->getElementsByTagName('tr');
foreach ($trs as $tr) 
{
   // now go through those elements. lookup the PHP doc on the DOM parser
   // You can iterate through sub-elements (like the 'td') just like through the 'tr'
}

You can also directly go for or just as you need it. Depending on the layout of your page you might want to go for a "higher" node so you know where you are based on tag parameters or the tag position.

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