PS: I can’t use DOM etc for this code as Xpath is not working for the html code, which contains huge number of errors from a poorly managed site. This will be easiest way for me.
I’ve the following html snippet from an erroneous html code:
<td width="11%">Train Number</Td>
<td width="16%">Train Name</td>
<td width="18%">Boarding Date <br>(DD-MM-YYYY)</td>
<td width="7%">From</Td>
<td width="7%">To</Td>
<td width="14%">Reserved Upto</Td>
<td width="21%">Boarding Point</Td>
<td width="6%">Class</Td>
</TR>
<TR>
<TD class="table_border_both">*12018</TD>
<TD class="table_border_both">DEHRADUN SHTBDI</TD>
<TD class="table_border_both"> 9- 9-2012</TD>
<TD class="table_border_both">DDN </TD>
<TD class="table_border_both">RK </TD>
<TD class="table_border_both">RK </TD>
<TD class="table_border_both">DDN </TD>
<TD class="table_border_both"> CC</TD>
I want to grab the values in the last 8 TD’s using regular expression. So if I place it within heredoc it does not match.
How should I place within heredoc so that this pattern( as is) matches?
I’m trying to do like this:
$trainpattern = <<<EOT
<td width="11%">Train Number</Td>
<td width="16%">Train Name</td>
<td width="18%">Boarding Date <br>[(]DD-MM-YYYY[)]</td>
<td width="7%">From</Td>
<td width="7%">To</Td>
<td width="14%">Reserved Upto</Td>
<td width="21%">Boarding Point</Td>
<td width="6%">Class</Td>
</TR>
<TR>
<TD class="table_border_both">[*]12018</TD>
<TD class="table_border_both">DEHRADUN SHTBDI</TD>
<TD class="table_border_both"> 9- 9-2012</TD>
<TD class="table_border_both">DDN </TD>
<TD class="table_border_both">RK </TD>
<TD class="table_border_both">RK </TD>
<TD class="table_border_both">DDN </TD>
<TD class="table_border_both"> CC</TD>
EOT;
$ret = preg_match("#$trainpattern#s",$filetext,$matches);
Also when I took only first two lines and joined them with \s+ into single line then it is matching but I’m looking for the way to match the lines without joining them at all. May be in that case I’d need to replace \n\r’s to \s*’s.
Have you tried phpQuery ? If you ever used jQuery, this will not be a problem.
Example :
Output :