PHP DOM Parser parse html table -
how use php dom parser parse content of table, get:
- the username
- the mobilephone number
- the status
so output of try extract be:
- randomusername - 0123456789 - active
- randomusername2 - 0987654321 - active
this html try parse (some part of it):
... <div class="table tbl-process-mobile"> <div class="table-cn"> <div class="table-bd"> <table cellspacing="0" id="idd7"> <thead> <tr id="idd9"> <th scope="col"> <span>username</span> </th> <th scope="col"> <span>status</span> </th> <th scope="col"> <span>prefered number</span> </th> <th scope="col"> <span>action</span> </th> </tr> </thead> <tbody id="iddb"> <tr class="even"> <td class="even"> <div>randomusername</div> </td><td class="odd"> <div>0123456789</div> </td><td class="even"> <div>active</div> </td><td class="odd"> <div> <span id="iddc" style="display:none"></span> <a href="xyz" id="idb2"><span>set number</span></a> </div> </td><td class="even"> <div> <a id="iddd" style="display:none"></a> <a href="xyz" class="action-icon-edit" id="idb3" title="change"> <i>change</i> </a> <a href="xyz" class="action-icon-delete" id="idb4" title="delete"> <i>delete</i> </a> </div> </td> </tr><tr class="odd"> <td class="even"> <div>randomusername2</div> </td><td class="odd"> <div>0987654321</div> </td><td class="even"> <div>active</div> </td><td class="odd"> <div> <span id="idde" style="display:none"></span> <a href="xyz" id="idb5"><span>set number</span></a> </div> </td><td class="even"> <div> <a id="iddf" style="display:none"></a> <a href="xyz" class="action-icon-edit" id="idb6" title="change"> <i>change</i> </a> <a href="xyz" class="action-icon-delete" id="idb7" title="delete"> <i>delete</i> </a> </div> </td> </tr> </tbody> </table> </div> </div> </div> ...
i started php code:
<?php error_reporting(0); $matches = array(); $dom = new domdocument; $dom->loadhtmlfile('settings.html');
how extract values, what's best way parse html point?
$field_names = ['username', 'phone', 'status']; $result = []; // search div tags having tbl-process-mobile class $containers = $doc->getelementsbytagname('div'); foreach ($containers $container) { if (!isset($container->attributes['class'])) continue; if (false === strpos($container->attributes['class']->value, 'tbl-process-mobile')) continue; // assume tbody tags required if (!$tbodies = $container->getelementsbytagname('tbody')) continue; // first tbody (there should not more) if (!$tbodies->length || !$tbody = $tbodies->item(0)) continue; foreach ($tbody->getelementsbytagname('tr') $tr) { $i = 0; $row = []; $cells = $tr->getelementsbytagname('td'); // collect first count($field_names) cell values maximum foreach ($field_names $name) { if (!$td = $cells->item($i++)) break; $row[$name] = trim($td->textcontent); } if ($row) $result []= $row; } } var_dump($result);
sample output
array(2) { [0]=> array(3) { ["username"]=> string(14) "randomusername" ["phone"]=> string(10) "0123456789" ["status"]=> string(6) "active" } [1]=> array(3) { ["username"]=> string(15) "randomusername2" ["phone"]=> string(10) "0987654321" ["status"]=> string(6) "active" } }
no comments, code self-explanatory.
p.s.: in sense of parsing, html structure leaves lot desired.
Comments
Post a Comment