LoginSignup
4
1

More than 3 years have passed since last update.

面倒なことはプログラミングで一発解決

Posted at

面倒なことはプログラミングで一発解決

index.php
id thumbnail name ruby area parent location latitude longitude kind type introduction description request score status

この横並びの非常に長い文字列を

index.php
id
thumbnail
name
ruby
area
parent
location
latitude
longitude
kind
type
introduction
description
request
score
status

このようにインデントを揃えたいとき、一つずつENTERで改行するの正直面倒です。

これを関数使ってスマートにやっていきましょう!

index.php
$str = 'id thumbnail name ruby area parent location latitude longitude kind type introduction description request score status';

$arr = explode(' ', $str);

var_dump($arr);

文字列を変数$strに代入し$strに対してexplode関数を適用します。

explode関数:第二引数の文字列を、第一引数で指定した区切り文字で区切りそれぞれ文字列を配列にする。
第一引数:区切り文字を指定
第二引数:文字列

このように表示されます。

index.php
array(16) {
  [0]=>
  string(2) "id"
  [1]=>
  string(9) "thumbnail"
  [2]=>
  string(4) "name"
  [3]=>
  string(4) "ruby"
  [4]=>
  string(4) "area"
  [5]=>
  string(6) "parent"
  [6]=>
  string(8) "location"
  [7]=>
  string(8) "latitude"
  [8]=>
  string(9) "longitude"
  [9]=>
  string(4) "kind"
  [10]=>
  string(4) "type"
  [11]=>
  string(12) "introduction"
  [12]=>
  string(11) "description"
  [13]=>
  string(7) "request"
  [14]=>
  string(5) "score"
  [15]=>
  string(6) "status"
}

これをforeachでループしechoすると
php:index.php
foreach ($arr as $column) {
echo $column . "\n";
}

index.php
id
thumbnail
name
ruby
area
parent
location
latitude
longitude
kind
type
introduction
description
request
score
status

するとこのように一発でインデントが揃えました。

私の上長の名言
人のすることじゃないと思ったら自動化すべし

ご閲覧ありがとうございました。

4
1
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
4
1