SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match_all help please!

    I have a .txt file that contains lines and lines of data that looks like this:


    [addy]a[endaddy],b,c,d,[price]$500[endprice],f,g,h
    [addy]a[endaddy],b,c,d,[price]$500[endprice],f,g,h
    [addy]a[endaddy],b,c,d,[price]$500[endprice],f,g,h
    [addy]a[endaddy],b,c,d,[price]$500[endprice],f,g,h
    [addy]a[endaddy],b,c,d,[price]$500[endprice],f,g,h

    I need some guidance as to how to loop though, and echo out the addy and price with one addy/price combo per line. this is a local file btw.

    thanks!

  2. #2
    SitePoint Enthusiast mrsmiley's Avatar
    Join Date
    Jul 2004
    Location
    Melbourne
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Forget about preg_match_all. Overkill for this job unless there is more to it than you are listing.

    http://au.php.net/fgetcsv

    It is CSV data after all

    If you have control over the txt file as well, then I'd recommend you drop the [addy][endaddy][price][endprice] stuff as well. All you need to know is the array offset that each column is to output it properly.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it would be better and easy to make the format of the data in the file as rows and columns separated by commas. If you would make it like:
    a,b,c,d,200,e,230
    a,b,c,d,210,e,240
    a,b,c,d,220,e,220
    You can directly read the lines and parse them easily to individual elements.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this maybe
    PHP Code:
    $text = < contents of the file >

    $re '/
        \[addy\](.*?)\[endaddy\]
        .*?
        \[price\](.*?)\[endprice\]
    /x'
    ;

    preg_match_all($re$text$mPREG_SET_ORDER);
    print_r($m); 
    The rest should be obvious.

  5. #5
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks all. Looks like fgetcsv will work for what I'm needing. I have only one problem... the price field will ALWAYS have a comma in it (i.e. $90,000) - and hence the price field gets split. I do have control of the text file though. Is there some way to get around this?

    And also - how do I go about displaying only the two fields that I need per row?

  6. #6
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The delimiter defaults to comma, but you can set it to something else, like pipes: | (see the link to the manual above).

    As for your second question: with a line such as a|b|c|d|200|e|230
    $data[0] will contain a, $data[1] will contain b, etc...

  7. #7
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - managed to put this together:

    PHP Code:
    <?php
    $row 
    1;
    $handle fopen("current_hud.txt""r");
    while ((
    $data fgetcsv($handle1000"|")) !== FALSE) {
       
    $num count($data);
       
    $row++;
       for (
    $c=0$c $num$c++) {
           echo 
    $data[0]." - ".$data[9]."<br />\n";
       }
    }
    fclose($handle);
    ?>
    It's getting the data I want, but I'm getting it multiple times, like so:

    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    1502 Airline Dr - $90,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000
    5240 Pinewood Terrace - $94,000

    etc.. etc...

    I only need each entry once. Any thoughts?

  8. #8
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think you don't need the for loop within the while loop.
    PHP Code:
    <?php
    $row 
    1;
    $handle fopen("current_hud.txt""r");
    while ((
    $data fgetcsv($handle1000"|")) !== FALSE) {
       
    $num count($data);
       
    $row++;
         echo 
    $data[0]." - ".$data[9]."<br />\n";
    }
    fclose($handle);
    ?>
    Last edited by mudshark; Mar 15, 2007 at 08:08. Reason: added code

  9. #9
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    956
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah... i just caught that... was headed back to repost. Thanks for all the help! It's looking exactly like I need it now.

    Thanks a ton!


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
  •