Click to See Complete Forum and Search --> : How to read from a txt file with data separated by tabs and ends of line?


Jazztronik
05-20-2008, 04:48 AM
For instance, if I got this txt file:

name\t John Silver\n
e-mail\t johnsilver@hotmail.com\n
phone\t 234 123 321\n

the \t's and \n's are not there actually, but they represent tabulations and ends of line. What I want is to separate the important data (in 2nd column) by using them as delimiters, instead of using characters like ";" or ",",....

What do you think would be the most proper solution?

NogDog
05-20-2008, 04:55 AM
fgetcsv (http://www.php.net/fgetcsv)() (see the optional 3rd parameter)

Jazztronik
05-20-2008, 05:18 AM
Thanks again NogDog for your quick and helpful replies. :) :) :)

I tried this solution before reading you, but it doesn't work, why??:

$data = file_get_contents("data.txt");

$sepData = explode("\n", $data);

for ($i = 0; $i < count($sepData); $i++) {
$sepData[$i] = strstr("\t", $sepData[$i]);
echo nl2br($sepData[$i]."\n"); // Just to view the array elements
}

It shows Asian characters. The file is coded in Unicode.

NogDog
05-20-2008, 01:44 PM
If using unicode, you will need to use the mbstring functions (http://www.php.net/mbstring), as PHP does not (yet) have native unicode support.