SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help parsing an email into PHP

    I have set up a script to import emails sent to a specific address on my server. I am trying to parse out some order numbers and associated tracking numbers. If anyone knows how to format the preg_match_all to grab what I want, I'd be very appreciative. Here is a short example of the email contents:



    <tr><td>08-13-17485</td><td>UPS</td><td><a rel="nofollow" target="_blank" href="http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&amp;tracknums_displayed=1&amp;TypeOfInquiryNumber=T&amp;loc=en_US&amp;InquiryNumber1=1Z1A094X0300222170&amp;track.x=0&amp;track.y=0">1Z1A094X0300222170</a></td></tr><tr><td>08-13-17486</td><td>UPS</td><td>
    <a rel="nofollow" target="_blank" href="http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&amp;tracknums_displayed=1&amp;TypeOfInquiryNumber=T&amp;loc=en_US&amp;InquiryNumber1=1Z9Y85770300051736&amp;track.x=0&amp;track.y=0">1Z9Y85770300051736</a></td></tr><tr><td>08-13-17488</td><td>UPS</td><td><a rel="nofollow" target="_blank"
    href="http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&amp;tracknums_displayed=1&amp;TypeOfInquiryNumber=T&amp;loc=en_US&amp;InquiryNumber1=1ZV46V540300097969&amp;track.x=0&amp;track.y=0">1ZV46V540300097969</a></td></tr


    Sorry for the jumble, but that's what it is... I'm formatting a regex string and basically want the results to be in an array that contains the order number and the tracking number. I've built a query to grab the order number, but I can't seem to get both the order number and the tracking number.

    Here's what I have so far:

    Code:
    preg_match_all('/<td>([0-9a-z\-\/]+)<\/td><td>UPS<\/td>', $email, $matches);
    It grabs the order number just fine, but I can't seem to get past the last </td> and on to the tracking number code before the query fails. I'm a regtard, so hopefully someone can help me and I'm not sure I've got the PHP right, either. I'd like the matches to be grouped together so that the order and tracking numbers are somehow tied together and easily accessible.

    PHP/Regex wizard needed!

    Thanks!

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a possibility expressed as a SimpleTest Test Case:

    PHP Code:
    class SitePointTest extends UnitTestCase {
        function 
    testExtractTrackingInfo() {
            
    $email '<tr><td>08-13-17485</td><td>UPS</td><td><a rel="nofollow" target="_blank" href="http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&amp;tracknums_displayed=1&amp;TypeOfInquiry Number=T&amp;loc=en_US&amp;InquiryNumber1=1Z1A094X0300222170&amp;track.x=0&amp ;track.y=0">1Z1A094X0300222170</a></td></tr><tr><td>08-13-17486</td><td>UPS</td><td>
    <a rel="nofollow" target="_blank" href="http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&amp;tracknums_displayed=1&amp;TypeOfInquiry Number=T&amp;loc=en_US&amp;InquiryNumber1=1Z9Y85770300051736&amp;track.x=0&amp ;track.y=0">1Z9Y85770300051736</a></td></tr><tr><td>08-13-17488</td><td>UPS</td><td><a rel="nofollow" target="_blank"
    href="http://wwwapps.ups.com/WebTracking/processInputRequest?sort_by=status&amp;tracknums_displayed=1&amp;TypeOfInquiry Number=T&amp;loc=en_US&amp;InquiryNumber1=1ZV46V540300097969&amp;track.x=0&amp ;track.y=0">1ZV46V540300097969</a></td></tr'
    ;
            
    $regex '~([^<>]+)</td><td>UPS</td><td><a[^>]+>([^<>]+)~ims';
            
    preg_match_all($regex,$email,$match);
            
    $expected1 = array('08-13-17485','08-13-17488');
            
    $expected2 = array('1Z1A094X0300222170','1ZV46V540300097969');
            
    $this->assertEqual($expected1,$match[1]);
            
    $this->assertEqual($expected2,$match[2]);
        }

    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works like a charm! Thanks for your help. I learn a little each time. Hopefully I'll be the one answering questions soon.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •