假设有一个名为name.txt
. 其中有一些内容如下:
Michal Guiles,Tanika Mcall,Jerry Sanipasi,Fallon Mcgillivray,Kurtis Bouras,Maria Teschner,Jerry Barbot,Earnestine
如何将所有名称放入名为的变量中$name
;然后逐一输出$comment->setname("$name");
阅读文件:
$file_handle = fopen("myfile.text", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
}
fclose($file_handle);
拆分零件:
$parts = explode(",",$line);
一张一张打印出来:
foreach ($parts as $part)
{
echo "$part<br/>";
}
您一次询问多件事。只做一件接一件的事:
阅读文件:(见file_get_contents
)
$text = file_get_contents('name.txt');
检索名称:(参见explode
)
$names = explode(',', $text);
遍历名称:(参见foreach
)
foreach ($names as $name)
{
$comment->setname($name);
}