LoginSignup
104
87

More than 5 years have passed since last update.

改行テキストを配列に変換する

Last updated at Posted at 2012-12-26
$text = 'a
b
c

d

f
';

といった空白行まじりの改行テキストを次の処理にかけると

$array = explode("\n", $text); // とりあえず行に分割
$array = array_map('trim', $array); // 各行にtrim()をかける
$array = array_filter($array, 'strlen'); // 文字数が0の行を取り除く
$array = array_values($array); // これはキーを連番に振りなおしてるだけ

空白行を取り除いたキレイな配列になる

array(5) {
  [0]=>
  string(1) "a"
  [1]=>
  string(1) "b"
  [2]=>
  string(1) "c"
  [3]=>
  string(1) "d"
  [4]=>
  string(1) "f"
}
104
87
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
104
87