以下是应该明确问题的代码。与此问题无关的代码已被忽略,例如获取网址的内容,将内容打印到新文件。
任何人都可以解释为什么变量是混乱的,除了最后一项?
<?php
$f = fopen("cucina2.txt", "r");
// Read line by line until end of file
while (!feof($f)) {
// Make an array using newline as delimiter
$arrM = explode("\n",fgets($f));
//Add the word to the url address
$url = 'hhttp://www.some.com/vocab/' . $arrM[0] . '/';
//Create a text file with the word
$glossary = $arrM[0];
$glossary .= '.txt';
//Check the functions
echo "arrM is: ";
echo $arrM[0];
echo "\n";
echo "glossary is: ";
echo $glossary;
echo "\n";
echo "url is: ";
echo $url;
echo "\n";
}
?>
cucina.txt:
batticarne
battuto
bavarese
bavetta
结果:
arrM is: batticarne
.txtsary is: batticarne
/rl is: hhttp://www.some.com/vocab/batticarne
arrM is: battuto
.txtsary is: battuto
/rl is: hhttp://www.some.com/vocab/battuto
arrM is: bavarese
.txtsary is: bavarese
/rl is: hhttp://www.some.com/vocab/bavarese
arrM is: bavetta
glossary is: bavetta.txt
url is: hhttp://www.some.com/vocab/bavetta/
答案 0 :(得分:0)
您的数据文件(cucina.txt)似乎包含Windows风格的换行符(&#34; \r\n
&#34;)。在\n
处拆分输入行时,在每个切片的末尾都会留下一个尾随回车符\r
。
请改为尝试:
$arrM = preg_split('/[\r\n]+/',fgets($f));
另外,请注意while (!feof($f))
通常是错误的。当您实际从文件中读取时,应检查EOF,而不是在循环顶部。这样你就可以避免处理空行。
while (!feof($f)) {
$s = fgets($f);
if (!$s) break;
$arrM = preg_split('/[\r\n]+/',$s);
:
:
}
答案 1 :(得分:0)
我刚刚运行了你的文件。然而,制作了我自己的单词列表。
我把一些我周围的东西放到一个文本文件中:
style
charity
new
samsung
asus
sharpie
tape
打印很好。返回
arrM is: style
glossary is: style.txt
url is: hhttp://www.some.com/vocab/style/
arrM is: charity
glossary is: charity.txt
url is: hhttp://www.some.com/vocab/charity/
arrM is: new
glossary is: new.txt
url is: hhttp://www.some.com/vocab/new/
arrM is: samsung
glossary is: samsung.txt
url is: hhttp://www.some.com/vocab/samsung/
arrM is: asus
glossary is: asus.txt
url is: hhttp://www.some.com/vocab/asus/
arrM is: sharpie
glossary is: sharpie.txt
url is: hhttp://www.some.com/vocab/sharpie/
arrM is: tape
glossary is: tape.txt
url is: hhttp://www.some.com/vocab/tape/
您的问题可能来自使用爆炸。如果您从中读取的文件已经是单词或一系列单词通过行返回表示您不需要爆炸字符串。只需逐行阅读文件,直到结束。
<?php
//create variable to iterate though the array until it is out of values.
$itter = 0;
$f = fopen("WordsList.txt", "r");
// Read line by line until end of file
while (!feof($f)) {
//read the text file line by line.
$line=fgets($f);
//Removed any whitespace left at the end of the line or whitespace generated from returning.
//Trim can be used to trim any character from a string though.
$line=trim($line);
//Add the line to the array.
$arrM[]=$line;
//Add the word to the url address
$url = 'http://www.some.com/vocab/' . $arrM[$itter] . '/';
//Create a text file with the word
$glossary = $arrM[$itter];
$glossary .= '.txt';
//Check the functions
echo "arrM is: ";
echo $arrM[$itter];
echo "<br />";
echo "glossary is: ";
echo $glossary;
echo "<br />";
echo "url is: ";
echo $url;
echo "<br />";
$itter++;
}
//After you read from a file always a food idea to close it/dump it from memory.
fclose($f);
?>
尝试该代码块。它使用trim fgets
逐行读取。然后从线的末端修剪任何空白区域。每次通过fgets拉出一条线时,它会修剪并将该线设置为等于变量$line
。然后,当读取并去除每一行时,它将获取该行并将其添加到您的数组中。
变量$itter
然后从零开始回读数组,直到它到达从文件生成的文件/数组中找到的行数的末尾。