2016-02-11 164 views
0

我必须将txt文件读入HTML表格。 它有很多领域,但我只想读“价值”字段。将txt文件读入HTML表格

这里是我的txt文件:

one=availability:, timestamp=90754, value=no 
two=description:, timestamp=074693, value=not sure 
three=Name, timestamp=90761, value=yes 

的一个,两个,三个值是我行标题,我想它的下面显示的值。

有无论如何使用iframe做到这一点? PHP不适合我。

回答

0

假设值总是最后一个字段,你是逐行读取文件中的行我会用肮脏的方法:

$value = explode('value=', $line)[1]; 
0

如果所有的行都跟着你大概可以相同的模式使用:

//$textfilestring = "one=availability:, timestamp=90754, value=no 
       //two=description:, timestamp=074693, value=not sure 
       //three=Name, timestamp=90761, value=yes"; 
$textfilestring = file_get_contents("PATH_TO_FILE"); 
$arraylines = explode("\n", $textfilestring); 

for ($i=0;$i<count($arraylines);$i++) { 
    $arraylines[$i] = explode("value=", $arraylines[$i]); 

} 
echo "<pre>"; 
var_dump($arraylines); 
echo "</pre>"; 

echo $arraylines[0][1] . "<br>"; 
echo $arraylines[1][1] . "<br>"; 
echo $arraylines[2][1] . "<br>"; 

$ arraylines应该是二维的一个部分beeing

one=availability:, timestamp=90754, 

和e beeing

no 

虽然未经测试。

+0

你怎么做arrayline 2个deimensiona?你能否给我提供一个很好的教程网站? – doctorwho11

+0

http://www.w3schools.com/php/php_arrays_multi.asp – Andreas

+0

@ doctorwho11对不起,代码中有一些错误。他们现在已经修好了。 – Andreas

0

冗长的方法

$headers=[]; 
$values=[]; 
$lines = file('./homework.txt', FILE_IGNORE_NEW_LINES); 
foreach($lines as $line){ 
    $chunks = explode(',',$line); 
     foreach($chunks as $index => $chunk){ 
      list($key, $value) = explode('=', trim($chunk)); 
      if(!$index){ 
       $headers[] = $value; 
      } 
      if('value' === $key){ 
       $values[] = $value; 
      } 
     } 
} 

echo "<table><thead><tr>"; 
foreach($headers as $h){  
    echo "<th>{$h}</th>"; 
} 
echo "</tr></thead><tbody><tr>"; 
foreach($values as $v){ 
    echo "<td>{$v}</td>"; 
} 
echo "</tr></tbody></table>";