Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    185
    Thanks
    17
    Thanked 0 Times in 0 Posts

    File Parsing Function

    Hi

    I need a way to parse a file based on the delimitation of either | tab or comma. Does anyone know the best way to do this that is the easiest? Also the file I am parsing might have a few empty strings between delimitation.

    I know about preg_split so if this is the only way could someone provide a link that explains regular expressions because i dont get it.

    Thanks
    Last edited by thekevin07; 03-06-2007 at 09:04 AM.

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,796
    Thanks
    19
    Thanked 156 Times in 147 Posts
    untested, but should give you the idea:

    PHP Code:
    $file file_get_contents($file_name);
    $file_array preg_split('/\||\t|,/',$file);
    foreach (
    $file_array as $key => $val)
    {
        if (empty(
    $val))
            unset(
    $file_array[$key])
    }
    $file_array2 array_values($file_array); 
    Links:

    http://us2.php.net/manual/en/function.preg-split.php
    http://www.regular-expressions.info/
    http://weblogtoolscollection.com/regex/regex.php
    Last edited by chump2877; 03-06-2007 at 10:28 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    unless you really need regex..
    PHP Code:
    <?php
    $lines
    =file($filename);
    foreach(
    $lines as $line){
        
    $bits=explode('|'$line);
        
    //do your stuff here
    }
    ?>
    if you expect pipes or commas in your files then explode may fail so look at something like fgetcsv which has support for delimiters etc
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    185
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks for the replies guys. What happens is i read each line in an array the first line contains fields that i need and i need to read these as the keys and then the delimited values in the second, third, fourth, etc line would be the values related to the keys now i think this association would be in a while loop or something but i cant seem to get my head around it. What i have come up with is below I also need that test to see if its empty I see that above so no need to put that in i can do it to save some time.


    Thanks Again


    PHP Code:

             $fcontents 
    file($target);
        
                 
    //get the fields
             
    $fields preg_split('/\||\t|,/',$fcontents[0]);

                
    //count fields
            
    $fCount=count($fields);
                
    //main loop to deal with array of elements
            
    $start=1;
            while(
    $start<=$fCount)
            {
        
                    
    //split the fields up
                
    $values=preg_split('/\||\t|,/',$fcontents[$start]);        
                     
                            
    ///SOURCE TO MATCH FIELDS TO VALUES GOES HERE????
                    

    Last edited by thekevin07; 03-06-2007 at 09:59 PM.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    As firepages suggested, don't use preg when you don't need it. It's bulky and slow.

    PHP Code:
    $lines file($filename);
    $list = array();
    foreach (
    $lines as $line) {
        
    $values explode('|'$line);
        if (
    sizeof($values) <= 1) continue; // 0 or only 1 element = empty, skip
        
    $list[$values[0]] = array_splice($values1); // only get everything after the first element

    I'm not sure if that is what you want though.

  • #6
    Regular Coder
    Join Date
    Dec 2006
    Posts
    185
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Hi

    actually i do need it i need to test for | commas and tabs either or and i may have more i just dont know what just yet im still in development. Can you tell me a little more on how your source code works and how it will work with my source code


    thanks
    Last edited by thekevin07; 03-06-2007 at 10:49 PM.


  •  

    Posting Permissions

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