在PHP中如何读取文本文件并将每行放入数组中?
我发现这个代码在某种程度上做了它,但是找了一个=标志,我需要寻找一个新的代码:
<?PHP
$file_handle = fopen("dictionary.txt", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);
print $parts[0] . $parts[1]. "<BR>";
}
fclose($file_handle);
?>
Run Code Online (Sandbox Code Playgroud)
好吧,你可以'='用a 代替,"\n"如果唯一的区别是你正在寻找换行符.
但是,更直接的方法是使用该file()功能:
$lines = file("dictionary.txt");
Run Code Online (Sandbox Code Playgroud)
这里的所有都是它的!
使用php的文件功能:
file - 将整个文件读入数组
例:
$lines = file('dictionary.txt');
echo $lines[0]; //echo the first line
Run Code Online (Sandbox Code Playgroud)