在PHP中解析HTML

dec*_*dev 5 html php parsing

我已经阅读了关于这个主题的其他帖子,但我似乎无法得到我想要的东西.

这是原始的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>
Run Code Online (Sandbox Code Playgroud)

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

这就是我现在所拥有的:

$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);
Run Code Online (Sandbox Code Playgroud)

但我得到一个空的 $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>');
Run Code Online (Sandbox Code Playgroud)

Mar*_*c B 5

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

$tempImported = $tempDom->importNode($selects, true);
Run Code Online (Sandbox Code Playgroud)

此时,$selects实际上是一个数组,您无法导入.你必须循环它并分别导入每个元素(结果节点).