Jump to content

[SOLVED] String Capturing


ainoy31

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

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.