1

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?

1
  • Wow, you guys reply quickly! I'm having a hard time following up here :) Commented Dec 9, 2009 at 14:31

6 Answers 6

6

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?

Sign up to request clarification or add additional context in comments.

2 Comments

It's the whole breaking the string apart that is my main concern right now.
+1 for simplicity. OP: Use explode("\t", $line, 2) to split the line on the first tab char only.
4
$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
    }
}

3 Comments

Consider passing 2 as the third argument to explode.
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?
do you really have 2 line breaks after each line? It sounds strange, but you said you have.
3

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

Comments

1

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.

3 Comments

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.
Looking at the other solutions, you're probably better off going for explode. Using RegEx here would probably overcomplicate things - my bad! :P
There's always room for some regex.
1

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.
  }
}

Comments

1

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.

2 Comments

The third parameter to explode() is $limit, which relieves you from worrying about tabs after the first one.
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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.