PHP读取txt数据文件并将数据传递到数组 [英] PHP reading a txt data file and passing data to array
问题描述
我需要从txt文件中提取以下数据(显示在链接中),然后保存mysql中指定的每个字段,我的问题是如何获取行中包含的每个字段,因为它由空格分隔
I need to extract the following data from a txt file(displayed in link),then save every field specified on mysql, my problem is how do I get every field contained in a row since its separated by spaces
我尝试过,
array = explode('',$ line);
,但是该方法节省了空间
I tried,
array = explode(' ', $line);
but that method saves spaces
我想将其放入一个数组中,
I would like to pu it into an array ,
Array=(
[0] => '1',
[ 1] => 'OC1',
[2] => 'Columna',
[3] => '1',
[4] => 'IN45X104',
[5] => '11745'
....
);
推荐答案
$string = preg_replace("/\s[\s]+/",",",$string);
$array = explode(",",$string);
preg_replace
就像 str_replace
,但允许您使用正则表达式 / \s [\s] + /
替换多个空格,然后全部空格替换为单个, ,因此现在很容易爆炸。
preg_replace
just work like str_replace
but allow you to replace with using regular expression /\s[\s]+/
matches multiple white spaces and then all that white spaces replace with single ","
so its easy to explode now.
函数来源: http://in3.php.net/manual/en/function.preg-replace。 php
这篇关于PHP读取txt数据文件并将数据传递到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!