[英]PHP get array of words from txt file
I have a text file with spam words. 我有一个包含垃圾邮件字词的文本文件。 I want to have an array filled with those words.
我想用这些单词填充一个数组。 I've tried doing:
我试着做:
$fp = @fopen("../files/spam.txt",'rb');
$words = fgetcsv($fp,100,"\n");
but it doesn't work (words only has the first letter of the txt file in it first cell). 但它不起作用(单词的第一个单元格中只有txt文件的第一个字母)。
do you how to do this? 你该怎么做?
EDIT: the .txt file looks like this: 编辑:.txt文件如下所示:
yahoo
google
msn
blah
blah
EDIT: I DONT KNOW WHAT IS A CSV FILE! 编辑: 我不知道这是CSV文件! THIS IS A TEXT FILE!
这是一个文本文件! I JUST GIVE AN EXAMPLE.
我举个例子。
please could some1 help me it looks really easy, i just dont understand. 请有人可以帮助我,它看起来真的很简单,我只是不明白。
您需要做的只是:
$words = file('./files/spam.txt',FILE_IGNORE_NEW_LINES);
That is not a CSV file. 那不是CSV文件。 CSV stands for comma separated values.
CSV代表逗号分隔的值。 You have no commas!
您没有逗号!
$spam_words = file('../files/spam.txt', FILE_IGNORE_NEW_LINES);
$artic = array(); //create a array
$directory = "/var/www/application/store/"; //define path
$files1 = scandir($directory); //scan the directory
$c = count($files1); //count the files the directory
print $c; //print it
for($i = 2; $i < $c; $i++) {
print "<br />" . $files1[$i];
$f = $directory . $files1[$i];
print $f . "<br />";
$h = fopen($f, 'r') or die("cannot open a file!!". $f);
$line1 = fgets($h);
list($id, $idval) = explode("\t\t", $line1);
print "$id";
}
How about splitting the string you read from the file into an array? 如何将从文件中读取的字符串拆分为数组?
split() function definition . split()函数定义 。
$string = "Niagara Becks Corn";
$array = split(" ", $string);
# ["Niagara", "Becks", "Corn"]
Use the 'file' function to read a PHP file from the disk into an array. 使用“文件”功能将PHP文件从磁盘读取到数组中。 See the manual here: http://php.net/manual/en/function.file.php
请参阅此处的手册: http : //php.net/manual/zh/function.file.php
Try this 尝试这个
$file = file_get_contents('spam.txt');
$file_words = explode(" ", $file);
$file_count = count($file_words);
for ($i=0; $i < $file_count; $i++){
echo $file_words[$i] . "<br />";
}
Your spam.txt file should look like this: 您的spam.txt文件应如下所示:
yahoo msn google
雅虎msn谷歌
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.