Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, since I can't see the full HTML, I'm not really sure if it's well formed, so I'll attempt to do this using more forgiving DOM functions. First off, I'm going to use this minimal html file as a sample:</p> <p><strong>test.html</strong></p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;select name="attrib1" class="Input"&gt; &lt;option value="0"&gt;&amp;nbsp;&lt;/option&gt; &lt;option value="140"&gt;140&lt;/option&gt; &lt;option value="141"&gt;150&lt;/option&gt; &lt;option value="142"&gt;160&lt;/option&gt; &lt;/select&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Now then, the first thing we need to do is create a DOM parser. We'll do this like so:</p> <pre><code>$doc = new DOMDocument(); $doc-&gt;loadHTMLFile("test.html"); </code></pre> <blockquote> <p>Okay, next we'll need to look at the requirements:</p> <p>I'm parsing and HTML page that contains a:</p> <pre><code>&lt;select&gt; &lt;option value="somevalue"&gt;Somedata&lt;/option&gt; &lt;/select&gt; </code></pre> <p>And I need to get both somevalue and somedata out of there.</p> </blockquote> <p>You also mention:</p> <blockquote> <p>Please note, the name is ALWAYS attrib1!</p> </blockquote> <p>Based on these requirements, I'm going to select all option tags that are a child of selects with the name "attrib1". To do so, I'm going to use something called XPath. This is a very flexible way to select dom elements based on specific conditions. Let's slowly build this out:</p> <pre class="lang-none prettyprint-override"><code>*/ select all elements */select select all elements that are select elements */select[@name='attrib1'] select all elements that are select elements with the name of attrib1 */select[@name='attrib1']/option select all select all option elements under all select elements with the name of attrib1 </code></pre> <p>Now then, we need to do this lookup, so we use the XPath functions:</p> <pre><code>$xpath = new DOMXpath($doc); $options = $xpath-&gt;query("*/select[@name='attrib1']/option"); foreach ($options as $option) { } </code></pre> <p>Now we need the value attribute, and the text inside. We'll first get the value attribute:</p> <pre><code>$optionValue = $option-&gt;getAttribute('value'); </code></pre> <p>Then we get what's inside the option tag:</p> <pre><code>$optionContent = $option-&gt;nodeValue; </code></pre> <p>And once we put this all together:</p> <pre><code>$doc = new DOMDocument(); $doc-&gt;loadHTMLFile("test.html"); $xpath = new DOMXpath($doc); $options = $xpath-&gt;query("*/select[@name='attrib1']/option"); foreach ($options as $option) { $optionValue = $option-&gt;getAttribute('value'); $optionContent = $option-&gt;nodeValue; echo "$optionValue and $optionContent\n"; } </code></pre> <p>We'll get the following output:</p> <pre><code>0 and   140 and 140 141 and 150 142 and 160 </code></pre> <p>And there you have it.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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