0
votes

I need to convert an HTML table (rows and columns) to PHP array. for example:

<tr>
   <th rowspan="2">Username</th>
   <th colspan="2">Personal</th>
</tr>
<tr>
    <th>Name</th>
    <th>Birth date</th>
</tr>

in PHP I need it to become:

array(
     [0] => array(
                 [0] => array(
                             ["value"] => "Username", 
                             ["rowspan"] => "2"),
                 [1] => array(
                             ["value"] => "Personal", 
                             ["colspan"] => "2")
                 ),
     [1] => array(
                 [0] => array(
                             ["value"] => "Name"
                             ),
                 [1] => array(
                             ["value"] => "Birth date"
                             )
                 )
);

So, the idea is that, the first array will keep the rows, inside each row I want to have an array of columns and inside of columns I want an array with the value of the cell and the attributes, I just need attributes like rowspan and colspan. so if you got the idea and know how to do it please share, I don't need you to do it for me, I just need to know how can I do this.

2
Why are you trying to do something? What you like to do so? - idmean
I don't know what you mean by "convert" Are you trying to parse this HTML into PHP? Does the HTML represent data that is POSTed to PHP? - Mike Brant
Convert it to json or xml in javascript, and then send it to php. - Pir Abdul
I developed a method that receives this type of array and then writes the values to an excel file using PHPExcel. The idea is, I have reports generated at runtime, so, the fields are chosen dynamically, and I cant do a static template using PHPExcel, so, I found a solution using arrays, and tested this array as an input of my method and it worked very well, I will improve the method. But now I have to know how to obtain this Html as an array. Pass the html to a php script is not a problem I want the idea to parse this html to an array. - Paulo Braga

2 Answers

0
votes

you should use combination of array like

array(
       array("rowspan"=>3,"colspan=>"2","class"=>"abc",id=>"row_1","value"=>array(1,2)
      );

the first array contains array for attriubtes and value array for displaying data

0
votes

Solution (1st Mar 2013)

So, here is the solution: first I send a form with a field containing the html table tags. and then I get the string containing the html, I am doing it with symfony, so in my action I wrote: $stringHeader = $request->getParameter('htmlTable'); htmlTable is the name of the field (input) from the form containing the html table then I convert the string to XML doing:

$xmlstring = <<<XML
<?xml version='1.0'?> 
<document>
$stringHeader
</document>
XML;

and finally put the data to the array, the structure of the html is as mentioned.

$xml = simplexml_load_string($xmlstring);
$header = array();
$columns = array();
foreach ($xml->tr as $tr) {
    foreach ($tr->th as $th) {
        if (isset($th->attributes()->rowspan)) {
            $rowspan = $th->attributes()->rowspan;
            $columns[] = array("value" => "$th", "rowspan" => "$rowspan");
        } elseif (isset($th->attributes()->colspan)) {
            $colspan = $th->attributes()->colspan;
            $columns[] = array("value" => "$th", "colspan" => "$colspan");
        } else {
            $columns[] = array("value" => "$th");
        }

    }
    $header[] = $columns;
    unset($columns);
    $columns = array();
}