我有以下txt文件:
++ ++结婚M + + ++乙NY ++
++ ++杰克++˚Fø++ ++ LS
写入.txt文件:
姓名:Marry
性别:M
血型:B
城市:纽约
姓名:杰克
性别:F
血型:O
城市:LS
我的代码是:
$fn = fopen("test.txt","r") or die("fail to open file");
$content = array();
while($row = fgets($fn)) {
$num = explode("++", $row);
$name = $num[0];
$sex = $num[1];
$blood = $num[2];
$city = $num[3];
}
$fp = fopen('output.txt', 'w');
fwrite($fp, $row);
fclose($fp);
这应该有用吗?因为它什么都不返回。
THX。
答案 0 :(得分:2)
$fn = fopen("test.txt","r") or die("fail to open file");
while($row = fgets($fn)) {
list( $sName, $sSex, $sBlood, $sCity ) = explode( "++", $row );
echo 'Name:' . $sName . '<br />';
echo 'Sex:' . $sSex . '<br />';
echo 'Blood type:' . $sBlood . '<br />';
echo 'City:' . $sCity . '<br />';
}
fclose( $fn );
要写入文件,您必须先创建一个缓冲区,然后写入该文件。最简单的方法是使用file_put_contents。请注意,file_put_content方法使用的内存比fopen,fwrite和fclose多。
$fn = fopen("test.txt","r") or die("fail to open file");
$sBuffer = '';
while($row = fgets($fn)) {
list( $sName, $sSex, $sBlood, $sCity ) = explode( "++", $row );
$sBuffer .= 'Name:' . $sName . PHP_EOL;
$sBuffer .= 'Sex:' . $sSex . PHP_EOL;
$sBuffer .= 'Blood type:' . $sBlood . PHP_EOL;
$sBuffer .= 'City:' . $sCity . PHP_EOL;
$sBuffer .= PHP_EOL; // There will be a empty line after each "set"
}
fclose( $fn );
file_put_contents( 'path/to/file.txt', $sBuffer );
使用fopen,fwrite和fopen。
$fn = fopen("test.txt","r") or die("fail to open file");
$rWrite = fopen( 'path/to/file.txt', 'w' ) or die( 'Could not open file for writing' );
while($row = fgets($fn)) {
list( $sName, $sSex, $sBlood, $sCity ) = explode( "++", $row );
fwrite( $rWrite, 'Name:' . $sName . PHP_EOL );
fwrite( $rWrite, 'Sex:' . $sSex . PHP_EOL );
fwrite( $rWrite, 'Blood type:' . $sBlood . PHP_EOL );
fwrite( $rWrite, 'City:' . $sCity . PHP_EOL );
fwrite( $rWrite, PHP_EOL ); // There will be a empty line after each "set"
}
fclose( $fn );
fclose( $rWrite );
答案 1 :(得分:1)
它没有输出任何内容,因为您没有使用$name
,$sex
等。尝试更改代码中的以下代码段,看看它现在的作用:
while($row = fgets($fn))
{
$num = explode("++", $row);
$name = $num[0];
$sex = $num[1];
$blood = $num[2];
$city = $num[3];
echo "$name $sex $blood $city<br>";
}
答案 2 :(得分:0)
你几乎是在正确的方向。您需要在与获取内容相同的循环中包含您的写入文件。
$fn = fopen("test.txt","r") or die("fail to open file");
$fp = fopen('output.txt', 'w') or die('fail to open output file');
while($row = fgets($fn)) {
$num = explode("++", $row);
$name = $num[0];
$sex = $num[1];
$blood = $num[2];
$city = $num[3];
fwrite($fp, "Name:$name\n");
fwrite($fp, "Sex:$sex\n");
fwrite($fp, "Blood:$blood\n");
fwrite($fp, "City:$city\n");
}
fclose($fn);
fclose($fp);
您的初始代码无效,因为fwrite需要一个字符串并且您正在传递一个数组。如果您确实传递了字符串,它将只包含test.txt
文件的最后一行。
答案 3 :(得分:0)
我有一个类似的问题,当[0]组件正确打印时,我无法打印“爆炸”结果的[1]组件。在我的情况下,我解决了更改while循环的方法:
while(!feof($myfile)) {
$row = fgets($myfile);
$components = explode(",", $row);
$myVar = $components[1];
echo $myVar;
}
到
while($row = fgets($myfile)) {
$components = explode(",", $row);
$myVar = $components[1];
echo $myVar;
}
答案 4 :(得分:0)
使用file_get_contents()
$row=file_get_contents("test.txt");
$list=explode("++",$row);
echo 'Name:'.$list[0].'<br>';
echo 'Sex:'.$list[1].'<br>';