Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Preg match all pulling out a <table> from the code

Posted on 2009-05-10
1
704 Views
Last Modified: 2012-05-06
Hello there

I m getting the code by using curl function I want to pull out the table that is from line number 72 to line number 327 in the code below I want the contents of the table from the second tr that is from line number 86 . Please assist me with the code on how to proceed with preg match all



<HTML>
<HEAD>
 
<Title>TRAIN NUMBERS</Title>
<HEAD>
<style type = "text/css">a:link{	color: blue; TEXT-DECORATION: none; }
a:visited{	color: rgb(0,153,153); } 
a:active {	color: rgb(255,102,0);}
p{	background-color: #E8E8D0; font-size: 20px;   FONT-FAMILY: arial,san-serif; FONT-SIZE: 10px; TEXT-DECORATION: none   color: #800000; }
body         { font-family: Verdana, Arial, Helvetica; background-color: FFFFFF;                color: #003366; font-size:10pt }
table        { table-border-color-light: rgb(00,153,153); table-border-color-dark:                rgb(00,153,153); background-color: #FFFFFF; font-size: 12px;                font-family: arial, san-serif; text-decoration:                none table-align Center; color: #800000;  }
h1, h2, h3, h4, h5, h6 { font-family:  Arial, Helvetica }
h1           
{ color: #800000; text-align: center; font-family: Arial; background-color:#FFFFFF;               font-size: 13pt; letter-spacing: 1pt; font-weight:                bold }
h2{	color: rgb(100,10,200); 	font-weight:bold;}
h3{	color: orange); }
h4           
{ font-family: arial, san-serif; text-decoration: none color #0066CC; color:                #FFFFFF; background-color: #003399; font-size: 10px }
h5           
{ color: #000066; font-size: 85%; text-align: Center; font-family: Arial }
h6           
{ color: #800080; font-family: Times New Roman; font-size: 10px;                letter-spacing: 1pt; text-align: Right; background-color:                #E8E8D0; font-style: italic; font-weight: bold }
.LegText     
{ color: #800000; font-family: arial, san-serif; font-size: 12px;                text-decoration: none; background-color: #FFFFFF }              
th           
{ background-color:#009999 ; font-size: 12px; font-family: arial, san-serif;                text-decoration: none; color: #FFFFFF }}
{ background-color:#009999 ; font-size: 12px; font-family: arial, san-serif;                text-decoration: none; color: #FFFFFF }}
 
</style>
</HEAD>
<BR>
 <BODY><table border="0" cellPadding="0" cellSpacing="0" width="95%"><tr>      <td align="center">         </td></tr></table>
 
<TABLE BORDER ALIGN=center >
<TR>
</TR>
</TABLE>
<TABLE border="0">
<td valign="top">
<H1 ALIGN = Center> TRAIN ROUTE </H1>
<TABLE BORDER ALIGN=center >
 <CAPTION> You Queried For </CAPTION>
<TR>
<TH>Train No</TH>
 
<TH>Train Name</TH>
<TH>Date</TH>
<TH>Runs From Source</TH>
<TH COLSPAN=7>Runs On</TH>
</TR>
<TR>
<TD>2727 </TD>
<TD>GODAVARI EXP   </TD>
<TD ALIGN = center>10/05/2009</TD>
<TD ALIGN = center>VISHAKAPATNAM  </TD>
 
<TD>MON </TD>
<TD>TUE </TD>
<TD>WED </TD>
<TD>THU </TD>
<TD>FRI </TD>
<TD>SAT </TD>
<TD>SUN </TD></TR>
</TABLE>
</td>
<td style="background-color: #FFFFFF" width="391" valign="top" rowspan="2">
 
<div id="narrow_ads_bottom"></div>
</td>
<tr>
<td valign="top">
<TABLE BORDER ALIGN=center>
<TR>
<TH>SNo</TH>
<TH>Stn Code</TH>
<TH>Stn Name</TH>
<TH>Route No.</TH>
<TH>Arrival Time</TH>
<TH>Dep. Time</TH>
 
<TH>Distance</TH>
<TH>Day</TH>
<TH>Remark</TH>
</TR>
<TR>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>VSKP</TD>
<TD>VISHAKAPATNAM  </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center><FONT COLOR = red>Source</TD>
 
<TD ALIGN = Center>17:25</TD>
<TD ALIGN = Center>0</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>2</TD>
<TD ALIGN = Center>DVD </TD>
<TD>DUVVADA        </TD>
<TD ALIGN = Center>1</TD>
 
<TD ALIGN = Center>17:53</TD>
<TD ALIGN = Center>17:54</TD>
<TD ALIGN = Center>18</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>3</TD>
<TD ALIGN = Center>AKP </TD>
<TD>ANAKAPALLE     </TD>
 
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>18:08</TD>
<TD ALIGN = Center>18:09</TD>
<TD ALIGN = Center>34</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>4</TD>
<TD ALIGN = Center>YLM </TD>
 
<TD>ELLAMANCHILI   </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>18:28</TD>
<TD ALIGN = Center>18:29</TD>
<TD ALIGN = Center>57</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>5</TD>
 
<TD ALIGN = Center>NRP </TD>
<TD>NARSIPATNAM RD </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>18:43</TD>
<TD ALIGN = Center>18:44</TD>
<TD ALIGN = Center>75</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
 
<TR>
<TD ALIGN = Center>6</TD>
<TD ALIGN = Center>TUNI</TD>
<TD>TUNI           </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>18:59</TD>
<TD ALIGN = Center>19:00</TD>
<TD ALIGN = Center>97</TD>
<TD ALIGN = Center>1</TD>
 
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>7</TD>
<TD ALIGN = Center>ANV </TD>
<TD>ANNAVARAM      </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>19:13</TD>
<TD ALIGN = Center>19:14</TD>
<TD ALIGN = Center>114</TD>
 
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>8</TD>
<TD ALIGN = Center>PAP </TD>
<TD>PITHAPURAM     </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>19:31</TD>
<TD ALIGN = Center>19:32</TD>
 
<TD ALIGN = Center>139</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>9</TD>
<TD ALIGN = Center>SLO </TD>
<TD>SAMALKOT JN    </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>19:44</TD>
 
<TD ALIGN = Center>19:45</TD>
<TD ALIGN = Center>151</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>10</TD>
<TD ALIGN = Center>APT </TD>
<TD>ANAPARTI       </TD>
<TD ALIGN = Center>1</TD>
 
<TD ALIGN = Center>20:04</TD>
<TD ALIGN = Center>20:05</TD>
<TD ALIGN = Center>177</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>11</TD>
<TD ALIGN = Center>RJY </TD>
<TD>RAJAMUNDRY     </TD>
 
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>20:41</TD>
<TD ALIGN = Center>20:45</TD>
<TD ALIGN = Center>201</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>12</TD>
<TD ALIGN = Center>NDD </TD>
 
<TD>NIDADAVOLU JN  </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>21:09</TD>
<TD ALIGN = Center>21:10</TD>
<TD ALIGN = Center>223</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>13</TD>
 
<TD ALIGN = Center>TDD </TD>
<TD>TADEPALLIGUDEM </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>21:27</TD>
<TD ALIGN = Center>21:28</TD>
<TD ALIGN = Center>243</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
 
<TR>
<TD ALIGN = Center>14</TD>
<TD ALIGN = Center>EE  </TD>
<TD>ELURU          </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>22:01</TD>
<TD ALIGN = Center>22:02</TD>
<TD ALIGN = Center>291</TD>
<TD ALIGN = Center>1</TD>
 
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>15</TD>
<TD ALIGN = Center>BZA </TD>
<TD>VIJAYAWADA JN  </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>23:40</TD>
<TD ALIGN = Center>23:55</TD>
<TD ALIGN = Center>350</TD>
 
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>16</TD>
<TD ALIGN = Center>KMT </TD>
<TD>KHAMMAM        </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>01:13</TD>
<TD ALIGN = Center>01:15</TD>
 
<TD ALIGN = Center>452</TD>
<TD ALIGN = Center>2</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>17</TD>
<TD ALIGN = Center>WL  </TD>
<TD>WARANGAL       </TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>02:46</TD>
 
<TD ALIGN = Center>02:48</TD>
<TD ALIGN = Center>559</TD>
<TD ALIGN = Center>2</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>18</TD>
<TD ALIGN = Center>KZJ </TD>
<TD>KAZIPET JN     </TD>
<TD ALIGN = Center>1</TD>
 
<TD ALIGN = Center>03:05</TD>
<TD ALIGN = Center>03:07</TD>
<TD ALIGN = Center>569</TD>
<TD ALIGN = Center>2</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>19</TD>
<TD ALIGN = Center>SC  </TD>
<TD>SECUNDERABAD JN</TD>
 
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>05:45</TD>
<TD ALIGN = Center>05:50</TD>
<TD ALIGN = Center>701</TD>
<TD ALIGN = Center>2</TD>
<TD ALIGN = Center>                </TD>
</TR>
<TR>
<TD ALIGN = Center>20</TD>
<TD ALIGN = Center>HYB </TD>
 
<TD>HYDERABAD DECAN</TD>
<TD ALIGN = Center>1</TD>
<TD ALIGN = Center>06:15</TD>
<TD ALIGN = Center><FONT COLOR = red>Destination</TD>
<TD ALIGN = Center>710</TD>
<TD ALIGN = Center>2</TD>
<TD ALIGN = Center>                </TD>
</TR>
</TABLE>
</td>
 
</tr>
</table>
 
</BODY>
</HTML>

Open in new window

0
Comment
Question by:navinbabu
1 Comment
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
ID: 24348746
Assuming that the variable $text holds the example html you have included, you can use this code:


<?php
 
# Define a partial regexp for <td>'s
$tdStart = '<TD[^>]*>\s*';
$tdEnd   = '\s*</TD>\s*';
 
# Construct the partial regexps for each column
$col[] = $tdStart.'(\d+)'.$tdEnd;
$col[] = $tdStart.'([a-z]+)'.$tdEnd;
$col[] = $tdStart.'([a-z ]+)'.$tdEnd;
$col[] = $tdStart.'(\d+)'.$tdEnd;
$col[] = $tdStart.'([\d\:]+)'.$tdEnd;
$col[] = $tdStart.'(.*?)'.$tdEnd;
$col[] = $tdStart.'(\d+)'.$tdEnd;
$col[] = $tdStart.'(\d+)'.$tdEnd;
 
# combine all columns into a expression that matches a full row (Except the last empty td and the last </tr>)
$preg = '#<TR>\s*'.implode('',$col).'#ims';
 
# Instantiate the array that will hold the results
$table = array();
 
# Execute the regular expression
if(preg_match_all($preg, $text, $matches)) {
        # Process the match results
	foreach($matches[0] as $index  => $row) {
		$table[] = array(
			@$matches[1][$index]
			,@$matches[2][$index]
			,@$matches[3][$index]
			,@$matches[4][$index]
			,@$matches[5][$index]
			,strip_tags(@$matches[6][$index])
			,@$matches[7][$index]
			,@$matches[8][$index]
		);
	}	
	
	echo '<pre>';
	print_r($table);
} else {
	echo 'No matches';
}
 
?>

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn how to count occurrences of each item in an array.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question