How to get the value of custom HTML attributes / custom attributes using PHP DOM Parser?

<li data-docid="thisisthevaluetoget" class="search-results-item"> </li> 

How to get the value of "data-docid"?

+1
source share
2 answers

You can use DOMDocument to get attributes :

 $html = '<li data-docid="thisisthevaluetoget" class="search-results-item"></li>'; $doc = new DOMDocument; $doc->loadHTML($html); $nodes = $doc->getElementsByTagName('li'); foreach ($nodes as $node) { if ($node->hasAttributes()) { foreach ($node->attributes as $a) { echo $a->nodeName.': '.$a->nodeValue.'<br/>'; } } } 
+2
source

You can do this using JavaScript + jQuery. You can get the value and transfer it to another php file using the $ _GET method.

example here

0
source

All Articles