LoginSignup
0
0

More than 3 years have passed since last update.

テキストファイルを読み込む、ファイルのテキストを1行ずつ処理する、テキストを分割して処理する

Posted at

PHPを使用してテキストファイルを読み込む

 1.テキストファイルとPHPファイルを「C:\xampp\htdocs\sample」に保存する
 
 2.PHPファイルを以下のように編集する

<!DOCTYPE html 
          PUBLIC" -//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="ja" lang="ja">
<head>
    <meta http-equiv="Content=Type" content="text/html; charset=UTF-8"/>
    <title>sample page</title>
</head>
<body>
     <h1>Hello PHP!</h1>
     <p><?php readfile("data.txt");?></p>
</body>      
</html>

確認

Googlechromeで「http://localhost/sample/index.php」にアクセス後
「テキストファイル読込成功!!」が表示されていることを確認する
image.png

今回使用した関数

今回使用したPHPのスクリプトは、ボディ部分に記載されている1行の関数のみです。
<?php readfile("data.txt");?>
この「readfile」というのがその後の()内に指定したファイルを読み込んでその場に書き出す関数です。テキストを表示したい場所に
これを書いておくだけで、そこにファイルの中身が書き出されます。
echoで出力しなくてもよい。ただし、複数行のテキストが書いてあっても
改行されないで表示される

ファイルのテキストを1行ずつ処理のプログラムを作成

index.phpを以下のように編集する

<?php
  $result ="";
  $lines = @file("data.txt") or $result ="ファイルが読めません.";
  if($lines != null){
       for($i=0;$i<count($lines);$i++){
           $result.=($i+1).":".$lines[$i]."<br>";
       }
  }
?>

<!DOCTYPE html 
          PUBLIC" -//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="ja" lang="ja">
<head>
    <meta http-equiv="Content=Type" content="text/html; charset=UTF-8"/>
    <title>sample page</title>
</head>
<body>
     <h1>Hello PHP!</h1>
    <p><?php echo $result; ?></p>
</body>      
</html>

確認

Googlechromeで「http://localhost/sample/index.php」にアクセス後
「〇行目読込成功」が表示されていることを確認する
image.png

今回使用した関数

 file関数
・処理内容
 「ファイルの中身をまるごと読込、1行ずつ配列にまとめて返す」
 
 $変数=file(ファイルの指定);

 このようにすると、引数に指定したファイルを読み込んで、1行ずつわけてまとめた配列を返します。 
 ※ただしファイルの読み込みに失敗した場合の処理はできない処理を追加する場合は以下の通り

 $変数=file(ファイルの指定)or 失敗時の処理;

テキストを分割して処理のプログラムを作成

 data.txtを以下のように編集する
 太郎,taro@yamada.jp,090-9999-9999
 花子,hanako@yamada.jp,090-8888-8888
 イチ,ichiro@yamada.jp,090-7777-7777

index.phpを以下のように編集

<?php
  $lines=@file("data.txt") or $result="ファイルが読めません";
  if($lines != null){
      $result ='<table border="0">';
      $result.="<tr><th>NAME</th><th>MAIL</th><th>TEL</th></tr>";
      for($i = 0;$i<count($lines);$i++){
          $result.="<tr>";
          $arr=explode(",",$lines[$i]);
          for($j=0;$j<3;$j++){
               $result.="<td>{$arr[$j]}</td>";
          }
          $result.="</tr>";
      }
      $result.="</table>";

  }
?>

<!DOCTYPE html 
          PUBLIC" -//W3C//DTD XHTML 1.0 Transitional//EN"
      "http://www.w3.org/TR/xhtmll/DTD/xhtmll-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="ja" lang="ja">
<head>
    <meta http-equiv="Content=Type" content="text/html; charset=UTF-8"/>
    <title>sample page</title>
    <style>
        h1{font-size:18pt;font-weight:bold;color:#666;}
        body{fontsize:13pt;color:#666;margin5px 25px;}
        tr{margin:5px;}
        th{padding:5px;color:white;background:darkgray;}
        td{padding:5px;color:black;background:#e0e0ff;}
    </style>
</head>
<body>
     <h1>Hello PHP!</h1>
    <p><?php echo $result; ?></p>
</body>      
</html>

確認

Googlechromeで「http://localhost/sample/index.php」にアクセス後
data.txtに保存した値が表示されていることを確認する
image.png

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0