Last active
February 19, 2016 11:07
-
-
Save suin/1500355 to your computer and use it in GitHub Desktop.
改行テキストを配列に変換する ref: http://qiita.com/suin/items/835ac0f58e6d3bbb6280
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$text = 'a | |
b | |
c | |
d | |
f | |
'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$array = explode("\n", $text); // とりあえず行に分割 | |
$array = array_map('trim', $array); // 各行にtrim()をかける | |
$array = array_filter($array, 'strlen'); // 文字数が0の行を取り除く | |
$array = array_values($array); // これはキーを連番に振りなおしてるだけ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
array(5) { | |
[0]=> | |
string(1) "a" | |
[1]=> | |
string(1) "b" | |
[2]=> | |
string(1) "c" | |
[3]=> | |
string(1) "d" | |
[4]=> | |
string(1) "f" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment