2013-05-14 103 views
0

我有一个文本文件,它有一个结构。我想使用PHP从结构中的键获取一些值并将它们添加到数组中。我想要的值是OBJECT。任何关于函数的建议来解析这个?PHP:解析结构化的txt文件

Object: 32.5445213,-92.0041885 
Icon: 0,0,000,6,6,"dude 1 (wx)\n2013-05-14 14:05:45 UTC\nSTATIONARY\nEmail: [email protected]\nWeb: www.dude.com\NMSCAS" 
Text: 15, 10, 1, "dude 1" 
End: 
Object: 34.2017555,-90.5699615 
Icon: 0,0,000,6,6,"dude 1 (wx)\n2013-05-14 14:05:45 UTC\nSTATIONARY\nEmail: [email protected]\nWeb: www.dude.com\NMSCAS" 
Text: 15, 10, 1, "dude 1" 
End: 
Object: 42.1689987,-88.2949982 
Icon: 0,0,000,6,6,"dude 1 (wx)\n2013-05-14 14:05:45 UTC\nSTATIONARY\nEmail: [email protected]\nWeb: www.dude.com\NMSCAS" 
Text: 15, 10, 1, "dude 1" 
End: 
Object: 48.1980019,-101.3073273 
Icon: 0,0,000,6,6,"dude 1 (wx)\n2013-05-14 14:05:45 UTC\nSTATIONARY\nEmail: [email protected]\nWeb: www.dude.com\NMSCAS" 
Text: 15, 10, 1, "KC0ISW" 
End: 
Object: 34.1373138,-88.7238998 
Icon: 0,0,000,6,6,"dude 1 (wx)\n2013-05-14 14:05:45 UTC\nSTATIONARY\nEmail: [email protected]\nWeb: www.dude.com\NMSCAS" 
Text: 15, 10, 1, "dude 1" 
End: 
+0

我会读(由线=行)的文件像往常一样,如果符合字符串“对象:”开始,我想借此串后的内容并把它。 – 2013-05-14 15:49:14

回答

2

做它几乎如果你想用C解析,如:与fscanf。这是最简单,最安全的

$file = fopen("filename", "r"); 
$data = array(); 
while (fscanf($file, "Object: %s\n", $object)) { 
    $data[] = $object; // explode(",", $object); // or this if you need to explode by the commas 
} 
+0

这看起来很直截了当,但我无法让它工作。我打开了txt文件。我做了一个fread,然后我粘贴你的代码,然后我打开,然后打印数组。生病的过去代码如下。 – 2013-05-14 21:12:39

+0

$ data = array(); $ file = fopen(“chasers.txt”,“r”); $ contents = fread($ file,filesize(“chasers.txt”)); (fscanf($ contents,“Object:%s”,$ object)){data {[] = $ object; } fclose($ file); print_r($ data); – 2013-05-14 21:13:10

+0

Nvm,我明白了。谢谢! – 2013-05-14 21:34:09