Php 读取所有行都带有分隔符的文本文件
我有一个如下所示的文本文件:Php 读取所有行都带有分隔符的文本文件,php,Php,我有一个如下所示的文本文件: NAME=ARTHUR LASTNAME=McConnell AGE=43 Array ( [NAME] => ARTHUR [LASTNAME] => McConnell [AGE] => 43 ) 我想用它做的是得到如下数组: NAME=ARTHUR LASTNAME=McConnell AGE=43 Array ( [NAME] => ARTHUR [LASTNAME] => McConnell [AGE] =&
NAME=ARTHUR
LASTNAME=McConnell
AGE=43
Array (
[NAME] => ARTHUR
[LASTNAME] => McConnell
[AGE] => 43
)
我想用它做的是得到如下数组:
NAME=ARTHUR
LASTNAME=McConnell
AGE=43
Array (
[NAME] => ARTHUR
[LASTNAME] => McConnell
[AGE] => 43
)
非常感谢您的帮助。您可以使用parse\u ini\u文件
或者使用爆炸两次
// first in end of lines
$data = explode(PHP_EOL, file_get_contents('myFile.txt'));
// and after, make a loop on the resulting array and creating a new array
$arr = array();
foreach ($data as $row) {
$line = explode("=", $row);
$arr[$line[0]] = $line[1];
}
如果您的文件格式具有完全相同的语法,则可以使用parse_ini_file。它不关心文件扩展名,所以只要格式正确,您也可以将其应用于.txt文件 test.txt parser.php
我想我已经在一个变量中给出了它
$variable = "NAME=ARTHUR
LASTNAME=McConnell
AGE=43"
//instead of this you can read the whole file
$lines = explode(PHP_EOL, $variable);
到目前为止,你试图做些什么来实现这一目标?你写了什么代码?您在这段代码中遇到了什么问题?您可以尝试将explode与循环www.php.net/explode结合使用
$variable = "NAME=ARTHUR
LASTNAME=McConnell
AGE=43"
//instead of this you can read the whole file
$lines = explode(PHP_EOL, $variable);
$filename = 'info.txt';
//Read the file into a line-by-line array
$contents = file($filename);
//Loop through each line
foreach($contents as $line) {
//Split by the = sign
$temp_array = explode('=', $line);
//Rebuild new array
$new_array[$temp_array[0]] = $temp_array[1];
}
//Print out the array at the end for testing
var_dump($new_array);