You are here:  » Parsing Pipe Separated Text File


Parsing Pipe Separated Text File

Submitted by jbregman on Sun, 2006-05-21 18:22 in

David,

I have a text file that has a format as follows

Item1|$$|127.0.0.1|33014|38837.0129173727|38858.7824140046|||100|0|0|
Item2|eA|IP1|58453|38858.517977338|38858.782236956|2.5.2285.22787|IME3|100|2|1|
Item3|da|IP2|58158|38858.3665680093|38858.7823904977||IME3|50|0|0|

I am trying to parse it with MagicParser (your pages say it should work with pipe separated files).

I am trying this:

require './lib/MagicParser.php';
$stats = array();

function myRecordHandler($record) {
global $stats;
$stats[] = $record;
}

$result = MagicParser_parse("filename.dat","myRecordHandler");

if (!$result) {
print MagicParser_getErrorMessage();
}

print_r($result);

But that only Returns 1.

Changing the line to $result = MagicParser_parse("filename.dat","myRecordHandler","|");

Returns "Invalid format string"

Can I make this work??

Jay

Submitted by support on Sun, 2006-05-21 18:34

Hi Jay,

It sounds like something is preventing the autodetection from working on your source file as I just pasted your sample text into the demo script and it parses correctly.....

http://www.magicparser.com/demo?fileID=4470B1EDB87D8&record=1

The format string that you should be using is "csv|124|0|0", so your call to MagicParser_parse should look like this:

$result = MagicParser_parse("filename.dat","myRecordHandler","csv|124|0|0");

If that still doesn't work, open the file into a text editor to confirm that no trailing white space or other characters are present.

Hope this helps!
David.

Submitted by jbregman on Sun, 2006-05-21 19:26

David,

Have just e-mailed you the complete file. I don't see any leading space and even when I delete the trailing space it still returns one with the exact code you listed.

Jay

Submitted by jbregman on Sun, 2006-05-21 19:37

Thanks,

Should be print_r($stats) not print_r($result). Works fine now with the code you gave.

Jay