SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot anchorwave's Avatar
    Join Date
    Nov 2003
    Location
    Tucson, Arizona
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advanced Text Delimited File

    I am looking to build a text delimited file by extracting values from a database. This isn't your standard tab or comma delimited format, rather each record is contained in it's own file.

    I have specific standards that determine both on what line a particular value must exist and between what clolumns. So for instance:

    Field: First name
    Must be placed on line three, starting at colum 4 and not extending past column 9.

    Code:
     123456789
    1
    2
    3    Mike
    4
    5
    6
    7
    8
    9
    I'm hoping to find some sort of PHP Class that could possible help me construct this and maybe another to interpret/validate a text file like this. Any ideas?

  2. #2
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why dont you extract the values and place them in an XML string. There are embedded XML parsers in PHP, that you can use. Look at http://www.php.net/manual/en/ref.xml.php.

    Hope that helps

  3. #3
    SitePoint Zealot anchorwave's Avatar
    Join Date
    Nov 2003
    Location
    Tucson, Arizona
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    XML would be ideal, unfortunately this file format standard is something that I have to conform to in order for it to work with some other applications that are already written. There is talk about transitioning to XML in the future, but for now I have to deal with this way of doing it.

  4. #4
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is a very strange situation. Perhaps you can get all your data from the database, and based on the conventions you declared above, build the files dynamicly.

    I don't think there are any libraries or classes available for the mentioned format.

  5. #5
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by anchorwave
    Field: First name
    Must be placed on line three, starting at colum 4 and not extending past column 9.

    Code:
     123456789
    1
    2
    3    Mike
    4
    5
    6
    7
    8
    9
    I'm hoping to find some sort of PHP Class that could possible help me construct this and maybe another to interpret/validate a text file like this. Any ideas?
    You can just draw what you need
    PHP Code:
        $format ""
    //     123456789123456789123
    /*1*/
    ."                    :\n"
    /*2*/."                    :\n"
    /*3*/."                    :\n"
    /*4*/."    fn____          :\n"
    /*5*/."    ln____          :\n"
    /*6*/."        age         :\n"
    /*7*/."        title__     :\n"
    /*8*/."                    :\n"

    and then substitute placeholders with real data:
    PHP Code:
    $data = array(
        
    'fn'   => 'Donald',
        
    'ln'   => 'Knuth',
        
    'age'  => 67,
        
    'title'=> 'The Art of Computer Programming'
    );
    $result preg_replace(
        
    "~([a-z]+)_*~e",
        
    ' substr(str_pad($data["$1"], 80), 0, strlen("$0"));',
        
    $format
    );
    echo 
    "<pre>$result</pre>"
    Don't know about "class"... Try asking in "advanced" forum

  6. #6
    SitePoint Zealot anchorwave's Avatar
    Join Date
    Nov 2003
    Location
    Tucson, Arizona
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That isn't a bad idea, thanks for giving me a fresh perspective on that.


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
  •