Solved

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

Posted on 2009-05-10
1
694 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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now