如何逐行阅读文本文件

时间:2012-09-12 20:40:39

标签: php

  

可能重复:
  How can I read and parse the contents of this text file?

我想读一个文本文件,因为C ++读了它

这是来自文本文件

的示例
(item(name 256) (Index 1)(Image "Wea001")   (specialty  (aspeed 700) (Hit 20)))
(item   (name 257)  (desc 520)           (Index 2)  (Image "Wea002")(specialty(Attack 16 24)))

我希望输出像

name : 256
Index : 1
Image : Wea001
Specialty > aspeed : 700 
Specialty > hit : 20

Name : 257
Desc : 520
Index : 2
Image : Wea002
Speciality > Attack : 16 24

这可能吗?

2 个答案:

答案 0 :(得分:1)

直接使用php手册:

fgets()

  

示例#1逐行读取文件

<?php
$handle = @fopen("/tmp/inputfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}
?>

答案 1 :(得分:0)

RTM

<?php
$f = fopen ("fgetstest.php", "r");
$ln= 0;
while ($line= fgets ($f)) {
    ++$ln;
    printf ("%2d: ", $ln);
    if ($line===FALSE) print ("FALSE\n");
    else print ($line);
}
fclose ($f);

&GT;

相关问题