LoginSignup
1
2

More than 5 years have passed since last update.

PHPのfile関数を文字列でも使うにはdataストリームラッパーを使うと簡単

Posted at

テキストファイルの内容を配列にしてくれるfile関数を文字列に対して使いたい場合は、dataストリームラッパーを経由すれば簡単。

<?php
$str = "あいうえお
abcde

12345
かきくけこ";

$arr = file("data://text/plain;base64," . base64_encode($str), FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
var_dump($arr);

/*
array(4) {
  [0]=>
  string(15) "あいうえお"
  [1]=>
  string(5) "abcde"
  [2]=>
  string(5) "12345"
  [3]=>
  string(15) "かきくけこ"
}
*/
1
2
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
1
2