views:

141

answers:

6

I'm reading a textfile on the format

Phone#(tab)Text(line break) (line break)

Or more specifically

4799999999  Hey! You owe us $576.53. Pay up to account no. 9760.00.12345, ID: 201561438.

4798888888  Hey! You owe us $199. Pay up to account no. 9760.05.12345, KID: 201565173.

4797777777  Hey! You owe us... and so on.

I want to store this in a SQL database, so I need to break this apart in phone numbers and text.

Any ideas on how to split the string into pairs in PHP? And should I store these values in a Array before saving to SQL, or should I just store it right away?

+6  A: 

Read the file line by line, split each line on the tab car, and then put it into the DB using suitable SQL for your DB platform.

Which part are you having trouble with?

Visage
It's the whole breaking the string apart that is my main concern right now.
Frode
+1 for simplicity. OP: Use `explode("\t", $line, 2)` to split the line on the *first* tab char only.
jensgram
+1  A: 

You could use Regular Expressions to break up the strings and then input them into the database using SQL.

^([0-9]{10})(.*)

would bring back the Phone Number as the first captured group, and the remaining text as the second captured group.

Daniel May
Uh, yeah. I've kinda figured you could do this with regex, but I have no idea how that works, really... I'm pretty new at PHP, and don't know anything about regex, except some idea about what you can use it for.
Frode
Looking at the other solutions, you're probably better off going for `explode`. Using RegEx here would probably overcomplicate things - my bad! :P
Daniel May
There's always room for some regex.
Peter Lindqvist
+3  A: 

Use the explode function with a tab (\t) delimiter on each line.

Ben S
+4  A: 
$data = file_get_contents ($file); // Assuming $file holds path to file
if ($data) {
    $lines = explode("\n\n", $data);
    foreach ($lines as $line) {
        list ($phone, $message) = explode("\t", $line, 2);
        // do whatever you need with it
    }
}
Deniss Kozlovs
Consider passing `2` as the third argument to `explode`.
jensgram
Thanks, added the limit.
Deniss Kozlovs
Okay, I've been trying this now, and it splits the string into the first phone#, then all the rest of the file. Is it possible the line break is different because of the files character encoding?
Frode
do you really have 2 line breaks after each line? It sounds strange, but you said you have.
Deniss Kozlovs
+1  A: 

Assuming the file isn't huge, you can just read the lines into an array with file() and iterate over them like so:

$lines = file($filename);

foreach($lines as $line) {
  $line_arr = explode("\t", $line, 2);

  if(!empty($line_arr)) {
    // $line_arr[0] will be the phone number and
    // $line_arr[1] will be the text; insert them into your database
    // however you please.
  }
}
Jordan
+1  A: 

I think you'd be better off using the strpos function, in case there are tabs in the text part.

$lines = file( $filename );

foreach ( $lines as $line )
{
    $tab = strpos( $line, "\t" );
    $num = substr( $line, 0, $tab );
    $text = substr( $line, $tab );
}

Replace the file function and foreach with fopen and fgets (see example) if the file is particularly large.

DisgruntledGoat
The third parameter to explode() is $limit, which relieves you from worrying about tabs after the first one.
Jordan
Looks like I learned something today, thanks Jordan. Actually I'd be interested in seeing which has better performance (though it would be pretty small difference TBH)
DisgruntledGoat