Extracting contents from a text file.

Discussion in 'PHP' started by M.Zeb Khan, Dec 24, 2008.

  1. #1
    Hi Guys!,

    I am working on a script which will extract contents of a text files to a MySQL database table. Look at this entry,

    The Problem is sometimes the first name is in different place, and the state is in different place, etc etc.....

    I have an idea what I am going to do is, write a function which will compare each entries and put it in its field. Forexample, it will search for email address and put it in the email field in the db table, will search for the address format and put it in the address format..... The main problem is firstname, lastname and city are same, so how can I compare it so it would enter the city in its own place and firstname and lastname in its own place?

    Help! much appreciated!!:eek:

    .
     
    M.Zeb Khan, Dec 24, 2008 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    You can't differentiate between values which are syntactically identical.

    Simply put, you can't do it without something truly ridiculous - I would recommend simply accepting user data to say which value is which.

    Also, it looks (and it is ;)) a delimited values file (comma in this case) - Does it not have a title row? Most have them, and in this case you can easily get the correct column title.

    Dan
     
    Danltn, Dec 24, 2008 IP
  3. M.Zeb Khan

    M.Zeb Khan Peon

    Messages:
    104
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hi Dan,

    Thanks for your response,

    Well, yes most of them have titles, any idea how to grab the entry per the title?, I can deal with commas via explode(), but title and comparison, help me :p

    .
     
    M.Zeb Khan, Dec 24, 2008 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    Just use fgetcsv.
     
    Danltn, Dec 24, 2008 IP
  5. apsam29

    apsam29 Peon

    Messages:
    68
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Use PHP regular expression for better performance of you text file processing.
    Do the followings,

    $string-data="email@test.com,John,Jackson,51 digitalpoint road,new york,CA,283928";

    1) Create a variable that stores all file contents(source file of data)
    2) Make a PHP regular expression - use preg_match_all($your-pattern,$string-data,$cahched-data). If it is one line make use of preg_match(....);
    3) Your regular expression look like this - $you-pattern="/(.+),(.+),(.+),(.+),(.+),(.+),(.+)/U"; (U is to Avoid Greedy lookups)
    4) the matches will be stored in $chached-data(change as you like).
    5) get $cached-data[1] - this is your email - process as you like, then the $cached-data[2] is your sencond value(name), the $cached-data[3] ... $cached-data[7] is your pin number
    6) Now all the data splitted for your convenience, make any changes then store it on Database or File System as you like

    Is it ok for you ..?
    If u have further doubts on it PM me.. I'll ready to help you ? because most of my PHP scripts have these functions to process file(s) data.
     
    apsam29, Dec 24, 2008 IP
  6. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Eww, Regex.

    <?php
    
    function parse( $data, $delimiter = ',', $enclosure = '"', $newline = "\n" ){ $pos = $last_pos = -1; $end = strlen( $data ); $row = 0; $quote_open = false; $trim_quote = false; $return = array(); for ( $i = -1;; ++$i ) { ++$pos; $comma_pos = strpos( $data, $delimiter, $pos ); $quote_pos = strpos( $data, $enclosure, $pos ); $newline_pos = strpos( $data, $newline, $pos ); $pos = min( ($comma_pos === false) ? $end : $comma_pos, ($quote_pos === false) ? $end : $quote_pos, ($newline_pos === false) ? $end : $newline_pos ); $char = ( isset($data[$pos]) ) ? $data[$pos] : null; $done = ( $pos == $end ); if ( $done || $char == $delimiter || $char == $newline ) { if ( $quote_open && !$done ) { continue; } $length = $pos - ++$last_pos; if ( $trim_quote ) { --$length; } $return[$row][] = ( $length > 0 ) ? str_replace( $enclosure . $enclosure, $enclosure, substr($data, $last_pos, $length) ) : ''; if ( $done ) { break; } $last_pos = $pos; if ( $char == $newline ) { ++$row; } $trim_quote = false; } else if ( $char == $enclosure ) { if ( $quote_open == false ) { $quote_open = true; $trim_quote = false; if ( $last_pos + 1 == $pos ) { ++$last_pos; } } else { $quote_open = false; $trim_quote = true; } } } return $return;}
    /* Very very fast, doesn't use any slow RegEx */
    
    $string = "email@test.com,John,Jackson,51 digitalpoint road,new york,CA,283928";
    $array = parse( $string );
    
    print_r($array);
    
    
    PHP:
    Returns:
    Array
    (
        [0] => Array
            (
                [0] => email@test.com
                [1] => John
                [2] => Jackson
                [3] => 51 digitalpoint road
                [4] => new york
                [5] => CA
                [6] => 283928
            )
    )
    Code (markup):
     
    Danltn, Dec 24, 2008 IP
    M.Zeb Khan likes this.
  7. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #7
    I think you can compare a particular name against states name and know the name of the state...

    that's one idea..
     
    Freewebspace, Dec 25, 2008 IP