Search code examples
phphtmlsimple-html-domuser-defined

How to parse user defined html attributes using html DOM with '-' in that attribute?


For example:

$html = "<div title='test a' class='a' classes='testing custom attribute with -' >test a</div>
         <div title='test b' class='b' >test b</div>
         <div title='test c' class='c' >test c</div>";

$htmldom = str_get_html($html);
$ab = $htmldom->find("div[class=a]");
foreach($ab as $e){
	echo $e->classes;
}

This gives me response like: "testing custom attribute with -"

But when i place '-' in custom attribute, like:

$html = "<div title='test a' class='a' class-es='testing custom attribute with -' >test first</div>
         <div title='test b' class='b' >test b</div>
         <div title='test c' class='c' >test c</div>";

$htmldom = str_get_html($html);
$div_a = $htmldom->find("div[class=a]");
foreach($div_a as $e){
	echo $e->class-es;
}

This is responding with an error:

"Notice: Use of undefined constant es - assumed 'es' in E:..."

Any help will be appreciated... Thanks in advance.


Solution

  • Seems php. http://php.net/manual/en/sdo.sample.getset.php

    Don't use object property access syntax, use the array index way: $e['class-es'] (...and maybe ->{'class-es'} could also work)