Note that there are some explanatory texts on larger screens.

plurals
  1. POParse HTML in PHP
    primarykey
    data
    text
    <p>I've read the other posts here about this topic, but I can't seems to get what I want.</p> <p>This is the original HTML:</p> <pre><code>&lt;div class="add-to-cart"&gt;&lt;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"&gt; &lt;div&gt;&lt;div class="attributes"&gt;&lt;div class="attribute attribute-1 odd"&gt;&lt;div id="edit-attributes-1-wrapper" class="form-item"&gt; &lt;label for="edit-attributes-1"&gt;Color: &lt;/label&gt; &lt;select id="edit-attributes-1" class="form-select" name="attributes[1]"&gt;&lt;option value="4"&gt;Blue&lt;/option&gt;&lt;option selected="selected" value="2"&gt;Brown&lt;/option&gt;&lt;option value="1"&gt;Tan&lt;/option&gt;&lt;/select&gt; &lt;/div&gt; &lt;/div&gt;&lt;div class="attribute attribute-2 even"&gt;&lt;div id="edit-attributes-2-wrapper" class="form-item"&gt; &lt;label for="edit-attributes-2"&gt;Rod Weight: &lt;/label&gt; &lt;select id="edit-attributes-2" class="form-select" name="attributes[2]"&gt;&lt;option selected="selected" value="5"&gt;5&lt;/option&gt;&lt;option value="6"&gt;6&lt;/option&gt;&lt;option value="7"&gt;7&lt;/option&gt;&lt;/select&gt; &lt;/div&gt; &lt;/div&gt;&lt;/div&gt;&lt;input type="hidden" value="1" id="edit-qty" name="qty"&gt; &lt;input type="submit" add_to_cart="{ &amp;quot;qty&amp;quot;: 1, &amp;quot;nid&amp;quot;: &amp;quot;20&amp;quot; }" class="form-submit node-add-to-cart ajax-submit-form" value="Add to cart" id="edit-submit-20" name="op"&gt; &lt;input type="hidden" value="form-688be703b34b0a9b0bb5bd98577ea203" id="form-688be703b34b0a9b0bb5bd98577ea203" name="form_build_id"&gt; &lt;input type="hidden" value="42cf9b00fa3c367125d06cbd4e033531" id="edit-uc-product-add-to-cart-form-20-form-token" name="form_token"&gt; &lt;input type="hidden" value="uc_product_add_to_cart_form_20" id="edit-uc-product-add-to-cart-form-20" name="form_id"&gt; &lt;input type="hidden" value="20" id="edit-pnid" name="pnid"&gt; &lt;/div&gt;&lt;/form&gt; &lt;/div&gt; </code></pre> <p>I only want to extract the two <code>&lt;select&gt;</code> tags and their contents.</p> <p>This is what I've got at the moment:</p> <pre><code>$dom = new DOMDocument(); $dom-&gt;loadHTML($node-&gt;content['add_to_cart']['#value']); // this loads the html above $selects = $dom-&gt;getElementsByTagName('select'); $tempDom = new DOMDocument(); $tempImported = $tempDom-&gt;importNode($selects, true); $tempDom-&gt;appendChild($tempImported); $output = $tempDom-&gt;saveHTML(); var_dump($output); </code></pre> <p>But I'm getting an empty <code>$output</code></p> <p><strong>Here is the working code:</strong></p> <pre><code>$dom = new DOMDocument(); $dom-&gt;loadHTML($node-&gt;content['add_to_cart']['#value']); $selects = $dom-&gt;getElementsByTagName('select'); $tempDom = new DOMDocument(); foreach ($selects as $select) { $tempImported = $tempDom-&gt;importNode($select, true); $tempDom-&gt;appendChild($tempImported); } $output = $tempDom-&gt;saveHTML(); print('&lt;div class="attributes"&gt;'. $output .'&lt;/div&gt;'); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload