-1

I want to extract all TD tag in separate array of TR of following Code

<TR>
<TD class="table_border_both"><B>Person 1</B></TD>
<TD class="table_border_both"><B>Start, 10</B></TD>
<TD class="table_border_both"><B>End  , 5</B></TD>
<TD class="table_border_both"><b>14
</b></TD>
</TR>
<TR>
<TD class="table_border_both"><B>Person 2</B></TD>
<TD class="table_border_both"><B>Start, 10</B></TD>
<TD class="table_border_both"><B>End  , 5</B></TD>
<TD class="table_border_both"><b>14
</b></TD>

I Tried This RegEx as follow

preg_match_all("/([<tr>|\\n]+(<td class=\"table_border_both\"><b>(.*?)<\\/b><\\/td>))/is", $str, $matches);

But I want to all TR in saprate array as follow

[0]=>
array(4) {
[0]=>string(12) "Person 1"
[1]=>string(19) "Start, 10"
[2]=>string(12) "End  , 5"
[3]=>string(7) "14
}
[1]=>
array(4) {
[0]=>string(12) "Person 2"
[1]=>string(19) "Start, 10"
[2]=>string(12) "End  , 5"
[3]=>string(7) "14
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Shivesh96
  • 1
  • 3

2 Answers2

1

Please don't attempt to parse HTML with regular expressions. It's not the right tool for the job.

PHP has a DOM extension for this very purpose. You could then use a simple XPath query to extract the elements you need.

There even are libraries which make this easier (list not exhaustive):

Community
  • 1
  • 1
Shira
  • 6,392
  • 2
  • 25
  • 27
1

I - like you - do nasty things with RegExes. But for now I'm not sure if you are serious about using Regular Expressions over DOM or just kidding around it, but as a part of community's prophecy, I'd like to introduce you to DOMDocument and its bro DOMXPath:

$document = new DOMDocument;
$document->loadHTML($html);
$xpath = new DOMXPath($document);
$trs = $xpath->query('//tr');
$array = [];
foreach ($trs as $key => $tr) {
    $td = $xpath->query('td', $tr);
    foreach ($td as $value) {
        $array[$key][] = $value->nodeValue;
    }
}
print_r($array);

Output:

Array
(
    [0] => Array
        (
            [0] => Person 1
            [1] => Start, 10
            [2] => End  , 5
            [3] => 14

        )

    [1] => Array
        (
            [0] => Person 2
            [1] => Start, 10
            [2] => End  , 5
            [3] => 14

        )

)

$html is your HTML source code

revo
  • 47,783
  • 14
  • 74
  • 117