Using php and regex to capture tags and data and store as an associative array

How can I use regex to find this table on the page (I need to find it by name):

<table id="Table Name">
<tr><td class="label">Name:</td>
<td class="data"><div class="datainfo">Stuff</div></td></tr>
<tr><td class="label">Email:</td>
<td class="data"><div class="datainfo">Stuff2</div></td></tr>
<tr><td class="label">Address:</td>
<td class="data"><div class="datainfo">Stuff3</div></td></tr>
</table>
<table id="Table Name 2">
<tr><td class="label">Field1:</td>
<td class="data"><div class="datainfo">MoreStuff</div></td></tr>
<tr><td class="label">Field2:</td>
<td class="data"><div class="datainfo">MoreStuff2</div></td></tr>
<tr><td class="label">Field3:</td>
<td class="data"><div class="datainfo">MoreStuff3</div></td></tr>
</table>

Then take the “labels” and “datainfo” and store them in an associative array, for example:

$table_name[name] //Stuff
$table_name[email] //Stuff2
$table_name[address] //Stuff3

$table_name2[field1] //MoreStuff
$table_name2[field2] //Morestuff2
$table_name2[field3] //Morestuff3
+3
source share
3 answers

Regexp is a bad solution in this case. Use a Simple HTML Parser instead .

Update: Here is the function for this:

 $html = str_get_html($html);
 print_r(get_table_fields($html, 'Table Name'));
 print_r(get_table_fields($html, 'Table Name 2'));

 function get_table_fields($html, $id) {
     $table = $html->find('table[id='.$id.']', 0);
     foreach ($table->find('tr') as $row) {
         $key = $row->find('td', 0)->plaintext;
         $value = $row->find('td', 1)->plaintext;
         ## remove ending ':' symbol
         $key = preg_replace('/:$/', '', $key);
         $result[$key] = $value;
     }
     return $result;
 }
+8
source

I have never played with a simple HTML parser, but I'm a pretty big fan of the built-in SimpleXML PHP. It does the same.

$XML = simplexml_load_string(file_get_contents('test_doc.html'));

$all_labels =  $XML->xpath("//td[@class='label']");
$all_datainfo = $XML->xpath("//div[@class='datainfo']");

$all = array_combine($all_labels,$all_datainfo);
foreach($all as $k=>$v) { $final[preg_replace('/:$/', '', (string)$k)] = (string)$v; }

print_r($final);

, (string), print_r $all.

:

Array
(
    [Name] => Stuff
    [Email] => Stuff2
    [Address] => Stuff3
    [Field1] => MoreStuff
    [Field2] => MoreStuff2
    [Field3] => MoreStuff3
)
0

I decided to create code using the PHP class DOMDocument

<?php 

$dom = new DOMDocument();

$dom->loadHTML(file_get_contents('stackoverflow_table.html'));

$count = 0;
$data = array();

while (++$count) {
  $tableid = 'Table Name' . ($count > 1 ? ' ' . $count : ''); //getting the table id  
  $table = $dom->getElementById($tableid);
  if ($table) {
    $tds = $table->getElementsByTagName('td');

    if ($tds->length) { //did I get td's? 
      for ($i = 0, $l = $tds->length;$i < $l; $i+=2) { 
        $keyname = $tds->item($i)->firstChild->nodeValue; //get the value of the firs td
        $value = null;
        if ($tds->item($i+1)->hasChildNodes()) //check if the 2º td has children (the div) (this might always be true because of whitespace)
          $value = $tds->item($i+1)->childNodes->item(1)->firstChild->nodeValue; //Get the div value (which is the second, because of whitespace)

        $data[$keyname] = $value;
      }
    }
  }
  else //there is no table
    break;
}

//should present the format you wanted :)
var_dump($data);

Here is the html file I created for this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Expires" content="Fri, Jan 01 1900 00:00:00 GMT">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Cache-Control" content="no-cache">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Lang" content="en">
<meta name="author" content="">
<meta http-equiv="Reply-to" content="">
<meta name="generator" content="">
<meta name="description" content="">
<meta name="keywords" content="">
<meta name="creation-date" content="11/11/2008">
<meta name="revisit-after" content="15 days">
<title>Example</title>
<link rel="stylesheet" type="text/css" href="my.css">
</head>
<body>
<table id="Table Name">
    <tr>
        <td class="label">Name:</td>
        <td class="data">
            <div class="datainfo">Stuff</div>
        </td>
    </tr>
    <tr>
        <td class="label">Email:</td>
        <td class="data">
            <div class="datainfo">Stuff2</div>
        </td>
    </tr>
    <tr>
        <td class="label">Address:</td>
        <td class="data">
            <div class="datainfo">Stuff3</div>
        </td>
    </tr>
</table>
<table id="Table Name 2">
    <tr>
        <td class="label">Field1:</td>
        <td class="data">
            <div class="datainfo">MoreStuff</div>
        </td>
    </tr>
    <tr>
        <td class="label">Field2:</td>
        <td class="data">
            <div class="datainfo">MoreStuff2</div>
        </td>
    </tr>
    <tr>
        <td class="label">Field3:</td>
        <td class="data">
            <div class="datainfo">MoreStuff3</div>
        </td>
    </tr>
</table>  
</body>
</html>
0
source

Source: https://habr.com/ru/post/1729867/


All Articles