SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    if ($zee == "Guru") { $zee--;}
    Join Date
    Nov 2005
    Location
    Karachi - Pakistan
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reading a Complex Text File

    Hello People !

    I need to read a text file that is not Comma Separated OR Tab Separated. Its a result of another system in a text format. Have a look at the contents in attached file. I am only interested in the data that is starting form LINE No. 9.

    Please guide me how to achieve this.


    I shall remain thankful !

    Zeeshan
    Attached Files Attached Files

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    Supposing that the data always starts at line 9, and is always formatted in the same way, you can read the file with file() and loop through the resulting array from line 9 (array key 8) until the end. Since there's no field separator, I guess you'll have to split each line in pieces using substr.

  3. #3
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    This is the closest I've gotten so far, the first entry is tripping me up.

    PHP Code:
    <?php
    $aParsedData 
    = array();
    foreach(
    file('path/to/your/file.txt') as $sLine)
    {
        if(
    === preg_match('~^([A-Z]{3}.+[0-9])$~'trim($sLine), $aMatches))
        {
            
    $aParsedData[] = preg_split('~\\s{2}\\s*~'$aMatches[0]);
        }
    }
    print_r($aParsedData);
    /*
    Array
    (
        [0] => Array
            (
                [0] => AAC
                [1] => AUSTRALIAN AGRICULTURAL COMPANY L FPO
                [2] => 293,548
                [3] => 14,953
                [4] => 5.09
                [5] => -6.55
                [6] => 264,264,459
                [7] => .00
            )

        [1] => Array
            (
                [0] => AAX
                [1] => AUSENCO LIMITED
                [2] => FPO
                [3] => 138,574
                [4] => 36,221
                [5] => 26.13
                [6] => -1.92
                [7] => 91,648,060
                [8] => .04
            )

        [2] => Array
            (
                [0] => ABB
                [1] => ABB GRAIN LIMITED
                [2] => FPO B
                [3] => 324,806
                [4] => 85,024
                [5] => 26.17
                [6] => -2.77
                [7] => 172,799,851
                [8] => .04
            )

        [3] => Array
            (
                [0] => ABC
                [1] => ADELAIDE BRIGHTON LIMITED
                [2] => FPO
                [3] => 592,202
                [4] => 110,789
                [5] => 18.70
                [6] => .59
                [7] => 552,488,537
                [8] => .02
            )

    )
    */
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  4. #4
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think guido is right. Because the fields are fixed width, it'll need to use substr() rather than any regexp to actually extract the field data from each row.
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint


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
  •