Help us understand the problem. What is going on with this article?

PHPの標準入力で値取得して表示するのによく使う処理(paizaスキルチェック用)

More than 1 year has passed since last update.

paizaラーニングのスキルチェック問題で、標準入力から得た値を整形したり、複数行を配列に入れるなど、よく使う処理を記録。どの問題でもだいたい使うと思います。

STDIN から 1 行読み込む

末尾の改行コードをtrimで取り除く。

$line = trim(fgets(STDIN)); 

複数行を取得して配列に格納

値を取得できる分だけループ。取得回数が決まってるならforで。

while ($line = fgets(STDIN)) {
   $array[] = trim($line);
}

値の途中に改行コードが存在する場合に取り除く

$line = str_replace(array("\r\n","\r","\n"), '', fgets(STDIN));

文字列のスペースで分割して配列に格納

$array = explode(" ", trim(fgets(STDIN)));

逆に、配列を文字列により連結するなら、implode

$string = implode ( "," , $array );
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした