ainoy31 Posted October 30, 2007 Share Posted October 30, 2007 Hello- I am trying to screen scrap data. The data on the screen is Del Date/Time: 10/24/07 2:30PM. I need to just get the date out. Here is what I have tried so far and no luck. Also, the date dynamicly changes. $line_number = 1; foreach(explode("\n", $data) as $line) { elseif(stristr($line, 'Del Date/Time:')) { $deliv_line = $line_number; $pattern = "/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/"; preg_match($pattern,$line, $matches); $deliv_date = date("Y-m-d", strtotime( $matches[0] ) ); $delivered = true; $status = 'Delivered'; ++$line_number; } Much appreciation. AM Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/ Share on other sites More sharing options...
effigy Posted October 30, 2007 Share Posted October 30, 2007 You have an elseif following a foreach--this will not run. Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381143 Share on other sites More sharing options...
ainoy31 Posted October 30, 2007 Author Share Posted October 30, 2007 I forgot to add this: <?php foreach(explode("\n", $data) as $line) { if(stristr($line, 'Pickup Date:')) { $pickup_line = $line_number; $pattern = "/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/"; preg_match($pattern,$line, $matches); $pu_date = date("Y-m-d", strtotime( $matches[0] ) ); } elseif(stristr($line, 'Del Date/Time:')) { $deliv_line = $line_number; $pattern = "/([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{1,2})/"; preg_match($pattern,$line, $matches); print_r($matches); $deliv_date = date("Y-m-d", strtotime( $matches[0] ) ); $delivered = true; $status = 'Delivered'; } ...etc. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381146 Share on other sites More sharing options...
effigy Posted October 30, 2007 Share Posted October 30, 2007 What does the data look like (including the surrounding data)? P.S. Please use code tags. Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381164 Share on other sites More sharing options...
ainoy31 Posted October 30, 2007 Author Share Posted October 30, 2007 I am using the curl method to process an online tracking form. I am able to send the data to the website fine. The problem is that I need to retrieve the data back from the result screen. The output source code from the website is: <tr> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Pro Number:</td> <td class="text" width='90' bgcolor='#f5f5dc'><b>8 1962284</b></td> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Shipper:</td> <td class="text" width='90' bgcolor='#f5f5dc'><b>VOGUE TIRE</b></td> </tr> <tr> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Ship City/St:</td> <td class="text" width='90' bgcolor='#f5f5dc'><b>MOUNT PROSPECT, IL</b></td> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Consgn City/St:</td> <td class="text" width='90' bgcolor='#f5f5dc'><b>MONTGOMERY, AL</b></td> </tr> <tr> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Pickup Date:</td> <td class="text" width='90' bgcolor='#f5f5dc'><b>10/24/07</b></td> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Del Date/Time:</td> <td class="text" width='90' bgcolor='#f5f5dc'><b>10/26/07<BR>2:30 PM</b></td> </tr> My code above gets me my Pickup Date but not the Del Date/Time -> print_r($matches) returns nothing. Hope this helps. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381172 Share on other sites More sharing options...
cooldude832 Posted October 30, 2007 Share Posted October 30, 2007 i'd do this <?php $string = ""; //What you posted above $data = strip_tags($string); $data = explode("\n",$data); ?> and then just pick the line number you want, I'm assuming the nubmer of lines is constant so $data[0] always is blank, $data[1] is always Pro Number, etc and since I striped tags, and exploded on lines it should be what u want. do a print_r($data) and see what u get Note you can use the strtotime() function to convert that text string into a date resource that can be reformated how you like (using the date flags) Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381176 Share on other sites More sharing options...
effigy Posted October 30, 2007 Share Posted October 30, 2007 <pre> <?php $data = <<<DATA <tr> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Pro Number:</td> <td class="text" width='90' bgcolor='#f5f5dc'>8 1962284</td> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Shipper:</td> <td class="text" width='90' bgcolor='#f5f5dc'>VOGUE TIRE</td> </tr> <tr> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Ship City/St:</td> <td class="text" width='90' bgcolor='#f5f5dc'>MOUNT PROSPECT, IL</td> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Consgn City/St:</td> <td class="text" width='90' bgcolor='#f5f5dc'>MONTGOMERY, AL</td> </tr> <tr> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Pickup Date:</td> <td class="text" width='90' bgcolor='#f5f5dc'>10/24/07</td> <td class="text" width='90' align='right' bgcolor='#f5deb3'>Del Date/Time:</td> <td class="text" width='90' bgcolor='#f5f5dc'>10/26/07<BR>2:30 PM</td> </tr> DATA; preg_match_all('#(Pickup Date|Del Date/Time):.*?(\d{2}/\d{2}/\d{2})#is', $data, $matches, PREG_SET_ORDER); print_r($matches); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381184 Share on other sites More sharing options...
cooldude832 Posted October 30, 2007 Share Posted October 30, 2007 oh you regex ppl, always find a work around with it. I like a generalized solution that lets you get more than a single line part. Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381187 Share on other sites More sharing options...
ainoy31 Posted October 30, 2007 Author Share Posted October 30, 2007 Thank you for all the help. Solution: foreach(explode("\n",$data) as $line) { if(stristr($line, 'Pickup Date:')) { preg_match_all('#(Pickup Date):.*?(\d{2}/\d{2}/\d{2})#is', $data, $matches, PREG_SET_ORDER); $pu_date = date("Y-m-d", strtotime( $matches[0][2] ) ); } if(stristr($line, 'Del Date/Time:')) { preg_match_all('#(Del Date/Time):.*?(\d{2}/\d{2}/\d{2})#is', $data, $matches, PREG_SET_ORDER); $deliv_date = date("Y-m-d", strtotime( $matches[0][2] ) ); $delivered = true; $status = 'Delivered'; } Quote Link to comment https://forums.phpfreaks.com/topic/75361-solved-string-capturing/#findComment-381274 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.