0

Hi I got an error on an old project which is using cURL and preg match to get the table contents. This is a taken part of a loop. so Its best to use preg_match to adapt the old codes. I have issues extracting the content. The columns is 12 but it failed to extract. Please help.

Thanks.

HTML :

 <table><tr>
//looping rows
<td align="center"><input type='checkbox' name="arr[]" value="17700002186"></td>
<input type='hidden' name="table_rg_17700002186" value="rg">
<input type='hidden' name="rg_id_17700002186" value="17700002186">
<input type='hidden' name="rg_uid_17700002186" value="18000174">
<input type='hidden' name="rg_date_17700002186" value="2014-08-22 12:11:37">
<td align="left">1 </td>
<td>ADMIN1 </td>
<td>2014-08-22 12:11:37</td>
<td> <a href="javascript:void(0);" onclick="window.open('http://www.testing.net/rg/popup/1/ADMIN1/1','','toolbar=1,status=1,location=1,scrollbars=1,resizable=1,width=1000,height=500,left='+(screen.availWidth/2-500)+',top='+(screen.availHeight/2-250)+'');"> Rgst </a></td>
<td>TESTING</td>
<td>BA</td>
<td>From : TESTING_<BR>
To: TEST1<BR>(873-021-0435,22-08-2014) </td>
<td align="right" style="padding-right:20px;">251,515</td>
<td align="right" style="padding-right:20px;"> 251515</td>
<td align="right" style="padding-right:20px;">0</td>
<td><textarea wrap="VIRTUAL" name="keter_17700002186" cols="10" row="2"></textarea></td>

// end of looped rows    
</tr>
//---------UPDATED HTML:-------------
<tr>
<td colspan="4" align=right>
<input type="submit" class="button" value="Accept" name="sbm"/></td>
<td colspan="4" align=right>
<input type="submit" class="button" value="Reject" name="sbm"/></td>
<td colspan="4">
<input type="submit" class="button" value="Delete" name="sbm"/>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="12">
<div style="float:right;"></div>
Total Record: <b>1</b>
</td>
</tr>
</tfoot>
 </table>
//---------UPDATED HTML:-------------

Preg_match:

preg_match('/\
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
.*\<td(.*)<\/td>
/simU',$html,$matches);

var_dump($matches); //array{}
xyonme
  • 395
  • 1
  • 7
  • 24

3 Answers3

2

I would recommend to use DOMDocument for this:

http://php.net/manual/en/domdocument.loadhtml.php

Example:

// ...
$dom = new DOMDocument;
$dom->loadHTML($html);
$cells = $dom->getElementsByTagName('td');
foreach ($cells as $cell) {
    echo $cell->nodeValue, PHP_EOL;
}
?>
smiggle
  • 1,259
  • 6
  • 16
  • This is a taken part of a loop so to adapt the old codes I need to use preg_match. Otherwise there would be a lot of changes. – xyonme Aug 22 '14 at 06:40
0

remove new line in regex try this

preg_match('/\.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>.*\<td(.*)<\/td>/simU',$html,$matches);

var_dump($matches);
Satish Sharma
  • 9,547
  • 6
  • 29
  • 51
0

You can use preg_split in conjucntion with array_slice:

$a_cells = array_slice(preg_split('/(?:<\/td>\s*|)<td[^>]*>/iu', $text), 1);

Demo

hindmost
  • 7,125
  • 3
  • 27
  • 39
  • This is very nice! I dont have to count columns. Nice but it includes the input button which is on the last row in the table – xyonme Aug 22 '14 at 07:07
  • @xyonme I don't understand. What's "the last row"? Your HTML only contains cells (columns), not rows. – hindmost Aug 22 '14 at 07:57
  • yes you are right! Yours is applicable to my question. on my codes, there is a loop and this question is just a part of the loop. there is a row containing buttons . because of preg match is matching with the patterns , it only selects the contents of the table and ignores those rows who didnt match the pattern. – xyonme Aug 22 '14 at 08:12
  • @xyonme So you had to describe your actual problem and post all your relevant code in the question – hindmost Aug 22 '14 at 08:21
  • @xyonme That's not valid HTML, it has unopened `table` and `tr` tags. Furthermore it's unclear what you want to get. Add the desired result into the question – hindmost Aug 22 '14 at 09:01
  • Hi. I have added the table tag. As you can see on the updated HTML , you can see 3 buttons on . Using your code , it will also include the other rows. The result that I want is only the looped rows. I hope you understand. – xyonme Aug 25 '14 at 09:45