Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Simple HTML DOM Parser: Accessing custom attributes
    primarykey
    data
    text
    <p>I want to access a custom attribute that I added to some elements in an HTML file, here's an example of the <code>littleBox="somevalue"</code> attribute</p> <pre><code>&lt;div id="someId" littleBox="someValue"&gt;inner text&lt;/div&gt; </code></pre> <p>The Following doesn't work:</p> <pre><code>foreach($html-&gt;find('div') as $element){ echo $element; if(isset($element-&gt;type)){ echo $element-&gt;littleBox; } } </code></pre> <p>I saw an article with a similar problem, but I couldn't replicate it for some reason. Here is what I tried:</p> <pre><code>function retrieveValue($str){ if (stripos($str, 'littleBox')){//check if element has it $var=preg_split("/littleBox=\"/",$str); //echo $var[1]; $var1=preg_split("/\"/",$var[1]); echo $var1[0]; } else return false; } </code></pre> <p>When ever I call the <code>retrieveValue()</code> function, nothing happens. Is <code>$element</code> (in the first PHP example above) not a string? I don't know if I missed something but it's not returning anything.</p> <p>Here's the script in it's entirety:</p> <pre><code>&lt;?php require("../../simplehtmldom/simple_html_dom.php"); if (isset($_POST['submit'])){ $html = file_get_html($_POST['webURL']); // Find all images foreach($html-&gt;find('div') as $element){ echo $element; if(isset($element-&gt;type)!= false){ echo retrieveValue($element); } } } function retrieveValue($str){ if (stripos($str, 'littleBox')){//check if element has it $var=preg_split("/littleBox=\"/",$str); //echo $var[1]; $var1=preg_split("/\"/",$var[1]); return $var1[0]; } else return false; } ?&gt; &lt;form method="post"&gt; Website URL&lt;input type="text" name="webURL"&gt; &lt;br /&gt; &lt;input type="submit" name="submit"&gt; &lt;/form&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