• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1174
  • Last Modified:

html table to array

I have an html table with that I'd like to convert to a php array.
Here is a representative row from the table:

 <tr>
 <td width=117 bgcolor="white" class="Normal"> <p align=center style='text-align:center'><span
  style='font-family:Arial'>Alvaro Gariban</span></p></td>

      <td width=41 bgcolor="white" class="Normal"> <p align=center style='text-align:center'><span
  style='font-family:Arial'>43</span></p></td>
</tr>

What I need to do is:

1)  relplace <tr> with ''  (empty)
2)  replace <td (attribute junk)> with ''  (empty)
3) replace </td> with '|'
4) get rid of the <p (attribute junk)></p> and <span (attribute junk)></span> tags
5) replace </tr> with \n

From here I can get it into php array, but I don't know regular expressions so I need help with steps #2 and #4


0
drew22
Asked:
drew22
2 Solutions
 
BatalfCommented:
Maybe something like this?


<?

$string = "<tr>
 <td width=117 bgcolor=\"white\" class=\"Normal\"> <p align=center style='text-align:center'><span
  style='font-family:Arial'>Alvaro Gariban</span></p></td>

      <td width=41 bgcolor=\"white\" class=\"Normal\"> <p align=center style='text-align:center'><span
  style='font-family:Arial'>43</span></p></td>
</tr>";

$string = preg_replace("/<T[DR][^>]*>/si","",$string); // remove all <TR> and <TD>
$string = preg_replace("/<\/TD>/si","|",$string);

$string = preg_replace("/<\/?(P|SPAN)[^>]*>/si","",$string);
$string = preg_replace("/<\/TR>/si","\n",$string);

echo nl2br(htmlentities($string));


?>
0
 
majestiqCommented:
$code = <<<CODE
<tr>
 <td width=117 bgcolor="white" class="Normal"> <p align=center style='text-align:center'><span
  style='font-family:Arial'>Alvaro Gariban</span></p></td>

      <td width=41 bgcolor="white" class="Normal"> <p align=center style='text-align:center'><span
  style='font-family:Arial'>43</span></p></td>
</tr>
CODE;

/* replace row and column starts with our place holders..
$code = str_replace("<tr", "~<tr", $code);
$code = str_replace("<td", "|<td", $code);

/* code sure we remove all of the html tags */
$code = strip_tags($code);

/* double explode */
$table = array();
$rows = explode("~", $code);
foreach ($rows as $key=>$val)
{
      $table[] = explode("|", $val);
}

// we have our array..
print_r($table);

Khurram Khan
{ domain name removed by Diablo84/PHP Page Editor }
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now