2014-03-26 16 views
0

我必须从应用程序导出的文本文件中更新数据。它看起来像这样:PHP从文本文件中获取数据(行和多行距划分)

customer001   1000   10  10000 
customer01   10000   100  1000000 
customer1   100  1000  100000 
customer0002  1000   1   1000 
customer012   1000   10  10000 

问题是我无法指定空间字符数ti saperate数据。

请建议,谢谢。

+0

是否有最小数量的空格字符? – SAMPro

回答

0

guneverdie,我用你的数据作为字符串。但是你可以使用php文件函数来读取你的应用程序导出文件。

$data = "customer001   1000   10  10000 
     customer01   10000   100  1000000 
     customer1   100  1000  100000 
     customer0002  1000   1   1000 
     customer012   1000   10  10000"; 


$customers_all = explode("\n", $data); 
$customer_individual = array(); 

foreach ($customers_all as $customer){ 

    $customer_with_spaces = explode(" ", $customer); 
    array_push($customer_individual, array_filter($customer_with_spaces));   
} 

print_r($customer_individual); 
+0

这很不错。谢谢。 – guneverdie

0

假设你有一个名为test.txt文件,其中包含您的数据:

你可以使用这个简单的awk命令:

awk '{print $1" "$2" "$3" "$4}' test.txt 

,并把你想要尽可能多的空间。在这里我只放了一个空间。其实你可以在这里使用任何字符作为分隔符。

试试这个:

awk '{print $1","$2","$3","$4}' test.txt 

这将增加值之间的逗号。看看什么适合你。

+0

我不明白这段代码。这是PHP代码,对不对? – guneverdie