我感觉我快到了。 这是一个 .txt 文件,大约 60 KB,里面全是德语单词。每个词都在一个新行上。
我想用这段代码遍历它:
<?php
$file = "GermanWords.txt";
$f = fopen($file,"r");
$parts = explode("\n", $f);
foreach ($parts as &$v)
{
echo $v;
}
?>
当我执行这段代码时,我得到:Resourceid#2
.txt里面没有resource这个词,不知道从哪里来的。
如何设法显示文本中的所有单词?
最佳答案
不需要 fopen
只需使用 file_get_contents
:
$file = "GermanWords.txt";
$contents = file_get_contents($file);
$lines = explode("\n", $contents); // this is your array of words
foreach($lines as $word) {
echo $word;
}
关于php - 在 PHP 中读取和迭代 .txt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12665233/