html - PHP DOM/xpath check elemet span class value -


within curl request have html table has below structure. want extract table rows contain span element empty class , not ones class="subcomponent". tried xpath find elements empty class how entire or better specific nodes contain version , partnumber. in advance.

<table> ... <tbody>     <tr>         <td></td>         <td></td>         <td>             <span class="">product</span>         </td>         <td>version</td>         <td>partnumber</td>     </tr>     <tr>         <td></td>         <td></td>         <td>             <span class="subcomponent">component</span>         </td>         <td>version</td>         <td>partnumber</td>     </tr> </tbody> 

my php code

$doc = new domdocument(); libxml_use_internal_errors(true); $doc->loadhtml($page); $doc->savehtml(); $xpath = new domxpath($doc); $query ='//span[@class=""]'; $entries = $xpath->query($query);  foreach ($entries $entry) {     echo $entry->c14n(); } 

to access table rows using simplexml, can use following:

$sxml = simplexml_load_string('<table>...</table>');  $rows = $sxml->xpath('//tr[td/span[@class=""]]');  foreach ($rows $row) {   echo "version: ", $row->td[3], ", partnumber: ", $row->td[4]; } 

the xpath works selecting <tr> tags have child <td>, has child <span> blank class.

in loop, need access child cells of each row number, since sample doesn't indicate they're labelled other way. i'm assuming table structure won't change though, should fine.

see https://eval.in/860169 example.

alternative domdocument version

if you're fetching full webpage, won't well-formed, might need use domdocument have in first example. it's bit less clean access child-elements, following work:

$doc = new domdocument; libxml_use_internal_errors(true); $doc->loadhtml($page); $xpath = new domxpath($doc); $rows = $xpath->query('//tr[td/span[@class=""]]');  foreach ($rows $row) {     $cells = $row->getelementsbytagname('td');      $version = $cells->item(3)->nodevalue;     $partnumber = $cells->item(4)->nodevalue;      echo "version: {$version}, part number: {$partnumber}", php_eol; } 

see https://eval.in/860217


Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -