Search code examples
phphtmlweb-scrapingsimple-html-dom

Unable to access value of attribute with dash in PHP using simple HTML DOM


I'm trying to fetch some html values with PHP Simple HTML DOM and store them in an PHP array.

Inside the HTML page I want to parse/fetch the following :

<li id="1" data-name="Jason" class="result-names">
<li id="2" data-name="John" class="result-names">
<li id="3" data-name="Elco" class="result-names">
<li id="5" data-name="Dana" class="result-names">

I am able to capture the "id" value and the "class" value, but at this moment I can't seem to get the value of "data-name". The code I am using for trying to achieve this :

<?php
require_once('simple_html_dom.php');
$dom = file_get_html('http://localhost/names.html');
foreach($dom->find('li[class=result-names]') as $results) {
    $item['id']     = $results->id;
    $item['name'] = $results->data-name;          //this does not work 
    $item['class'] = $results->class;
    $articles[] = $item;
}

echo "<pre>";
print_r($articles);
echo "</pre>";

Solution

  • Use:

    $item['name'] = $results->getAttribute("data-name");
    

    - is the subtraction operator, it can't be used as part of an identifier. What you write is parsed as:

    $item['name'] = ($results->data) - name;