我有这段代码
<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>
如何将信息提取到数组中,以便我有这样的东西..
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个
答案 0 :(得分:1)
您可以尝试使用DOMDocument类,但必须修复html。您有结束链接标记(</a>
),没有任何开始链接标记(<a href="">
)
答案 1 :(得分:0)
<?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];
?>
答案 2 :(得分:0)
看一下这段代码:
$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'
}
您也可以直接选择或根据需要选择。根据页面的布局,您可能需要选择“更高”的节点,以便根据标签参数或标签位置知道您的位置。