用PHP解析HTML [英] Parse HTML in PHP

查看:91
本文介绍了用PHP解析HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这里阅读了有关该主题的其他文章,但是我似乎无法获得想要的东西.

I've read the other posts here about this topic, but I can't seems to get what I want.

这是原始HTML:

<div class="add-to-cart"><form class=" ajax-cart-form ajax-cart-form-kit" id="uc-product-add-to-cart-form-20" method="post" accept-charset="UTF-8" action="/product/rainbox-river-lodge-guides-salomon-selection">
<div><div class="attributes"><div class="attribute attribute-1 odd"><div id="edit-attributes-1-wrapper" class="form-item">
 <label for="edit-attributes-1">Color: </label>
 <select id="edit-attributes-1" class="form-select" name="attributes[1]"><option value="4">Blue</option><option selected="selected" value="2">Brown</option><option value="1">Tan</option></select>
</div>
</div><div class="attribute attribute-2 even"><div id="edit-attributes-2-wrapper" class="form-item">
 <label for="edit-attributes-2">Rod Weight: </label>
 <select id="edit-attributes-2" class="form-select" name="attributes[2]"><option selected="selected" value="5">5</option><option value="6">6</option><option value="7">7</option></select>
</div>
</div></div><input type="hidden" value="1" id="edit-qty" name="qty">
<input type="submit" add_to_cart="{ &quot;qty&quot;: 1, &quot;nid&quot;: &quot;20&quot; }" class="form-submit node-add-to-cart ajax-submit-form" value="Add to cart" id="edit-submit-20" name="op">
<input type="hidden" value="form-688be703b34b0a9b0bb5bd98577ea203" id="form-688be703b34b0a9b0bb5bd98577ea203" name="form_build_id">
<input type="hidden" value="42cf9b00fa3c367125d06cbd4e033531" id="edit-uc-product-add-to-cart-form-20-form-token" name="form_token">
<input type="hidden" value="uc_product_add_to_cart_form_20" id="edit-uc-product-add-to-cart-form-20" name="form_id">
<input type="hidden" value="20" id="edit-pnid" name="pnid">

</div></form>
</div>

我只想提取两个<select>标签及其内容.

I only want to extract the two <select> tags and their contents.

这是我目前所拥有的:

$dom = new DOMDocument();
$dom->loadHTML($node->content['add_to_cart']['#value']);  // this loads the html above
$selects = $dom->getElementsByTagName('select');

$tempDom = new DOMDocument();
$tempImported = $tempDom->importNode($selects, true);
$tempDom->appendChild($tempImported);
$output = $tempDom->saveHTML();
var_dump($output);

但是我得到一个空的$output

这是工作代码:

$dom = new DOMDocument();
$dom->loadHTML($node->content['add_to_cart']['#value']);
$selects = $dom->getElementsByTagName('select');

$tempDom = new DOMDocument();
foreach ($selects as $select) {
  $tempImported = $tempDom->importNode($select, true);
  $tempDom->appendChild($tempImported);
}

$output = $tempDom->saveHTML();
print('<div class="attributes">'. $output .'</div>');

推荐答案

dom->getElementsByTagName()返回其结果作为数组,所以...

dom->getElementsByTagName() returns its results as an array, so...

$tempImported = $tempDom->importNode($selects, true);

至此,$selects实际上是一个数组,您无法导入.您必须对其进行循环,然后分别导入每个元素(结果节点).

at this point, $selects is actually an array, which you can't import. You'll have to loop over it and import each element (the result nodes) seperately.

这篇关于用PHP解析HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆