kevin87 Posted January 26, 2014 Share Posted January 26, 2014 Hi everyone, I need some help pulling data from a remote site. Currently I am using the following code which works and pulls all of the data. The next step is where I am a little lost. $str = file_get_contents("http://www.airnav.com/airport/KARR"); I would like to only pull the following data from the remote site. <A name="ops"></A><H3>Airport Operations</H3> <TABLE cellpadding=0 cellspacing=0 border=0> <TR><TD nowrap valign=top align=right>Airport use: </TD><TD valign=top>Open to the public</TD></TR> <TR><TD nowrap align=right>Activation date: </TD><TD>04/1966</TD></TR> <TR><TD nowrap valign=top align=right>Sectional chart: </TD><TD valign=top><A href="/sectionals">CHICAGO</A></TD></TR> <TR><TD nowrap align=right>Control tower: </TD><TD>yes</TD></TR> Can anyone help me out with this? Thanks, Kevin Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 26, 2014 Share Posted January 26, 2014 (edited) Find what you are looking for using regex $html = file_get_contents("http://www.airnav.com/airport/KARR"); preg_match('~<a name="ops"></a>.+</h3>(.*?)</table>~is', $html, $matches); $targetData = trim($matches[1]); echo $targetData; If you dont want the html included then apply strip_tags to $targetData; Edited January 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
kevin87 Posted January 26, 2014 Author Share Posted January 26, 2014 Thanks a lot for the response. I ran this code and had a little trouble, but I think it is getting closer. Maybe I can understand it if there is less data. What if I just wanted to grab this string and print it? <A name="ops"></A><H3>Airport Operations</H3> Quote Link to comment Share on other sites More sharing options...
kevin87 Posted January 26, 2014 Author Share Posted January 26, 2014 Also a second question. Is there a way to print only line 75 to 100 when using $html = file_get_contents("http://www.airnav.com/airport/KARR"); Thanks, Kevin Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted January 26, 2014 Solution Share Posted January 26, 2014 (edited) I have issue with my regex pattern, change it to preg_match('~<a name="ops"></a>(.*?)</table>~is', $html, $matches); $targetDate will contain <H3>Airport Operations</H3> <TABLE cellpadding=0 cellspacing=0 border=0> <TR><TD nowrap valign=top align=right>Airport use: </TD><TD valign=top>Open to the public</TD></TR> <TR><TD nowrap align=right>Activation date: </TD><TD>04/1966</TD></TR> <TR><TD nowrap valign=top align=right>Sectional chart: </TD><TD valign=top><A href="/sectionals">CHICAGO</A></TD></TR> <TR><TD nowrap align=right>Control tower: </TD><TD>yes</TD></TR> <TR><TD valign=top align=right>ARTCC: </TD><TD valign=top>CHICAGO CENTER</TD></TR> <TR><TD valign=top align=right>FSS: </TD><TD valign=top>KANKAKEE FLIGHT SERVICE STATION</TD></TR> <TR><TD nowrap valign=top align=right>NOTAMs facility: </TD><TD valign=top>ARR (NOTAM-D service available)</TD></TR> <TR><TD align=right>Attendance: </TD><TD>0600-2400</TD></TR> <TR><TD nowrap align=right>Wind indicator: </TD><TD>lighted</TD></TR> <TR><TD nowrap align=right>Segmented circle: </TD><TD>no</TD></TR> <TR><TD valign=top align=right>Lights: </TD><TD valign=top>WHEN ATCT CLSD HIRL RYS 15/33 & 09/27 & MIRL RY 18/36 PRESET LOW INTST; TO INCR INTST & ACTVT MALSR RYS 09, 33; REIL RYS 18;36,15,33 & 27, AND TWY LGTS - CTAF.</TD></TR> <TR><TD valign=top align=right>Beacon: </TD><TD valign=top>white-green (lighted land airport)<BR>Operates sunset to sunrise.</TD></TR> Edited January 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted January 26, 2014 Share Posted January 26, 2014 (edited) Also a second question. Is there a way to print only line 75 to 100 when using If you want specific lines, then you could do $html = file("airport.html"); $targetData =''; // start at line 75 and loop till line 100 for($i = 74; $i <= 99; $i++) { $targetData .= $html[$i]; // add html to variable } echo $targetData; // echo html Edited January 26, 2014 by Ch0cu3r Quote Link to comment Share on other sites More sharing options...
kevin87 Posted January 26, 2014 Author Share Posted January 26, 2014 Thanks a lot, you rock. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.