Search code examples
phpsimple-html-dom

PHP Simple HTML Dom hyphenated attribute with "find"


I cannot get PHP Simple HTML Dom to get data from custom data attributes.

The code changes I've made just return all kinds of elements and data, and not just the custom data attributes as per the code - eg data-XYZ.

I've read other questions/answers which suggest using {} but I cannot get it working no matter how I structure curly braces around quotes, apostrophes, square braces, etc.

Example of HTML I'm trying to extract data from:

    <!-- Data I want to extract -->

    <li class="main-li" 
       data-name="Eric"
       data-info="loves bananas"
       data-address="29 Acacia Road"
    >

<!-- Data I'm actually getting -->
    <div class="float-main">
       <p class="telephone">DATA A</p>
    </div>

    <h3 class="name">
        <a href="some_link" title="some title">DATA B</a>
    </h3>
    <p class="more-data">DATA C</p>
    <p class="address">DATA D</p>

    </li>

Current usage of Simple HTML Dom:

    foreach( $html->find('[data-name]') as $data ) 
      {
        echo $data->plaintext;
        echo "<br>";
      }

I want the data from the custom attributes in the li:
data-name
data-info
data-address

What I'm currently getting is DATA A, B, C, D - from the h3 link, and the <p> areas.

If I need to simply use {}. how do I apply those to $html->find('[data-name]')?


Solution

  • It needs to look like this:

    $html->find('[data-name]', 0)->{'data-name'}