Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/75361-solved-string-capturing/
Share on other sites

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.
}
?>

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.

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)

<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>

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';

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.