LoginSignup
36
24

More than 3 years have passed since last update.

Laravel5.8 文字列をキャメルケース・パスカルケース・スネークケース・ケバブケース・単数系・複数形に変換する

Last updated at Posted at 2019-08-05

LaravelやPHPに標準搭載されているいい感じの関数を使って文字列を自由自在に変換します。

Laravel5.8以降は文字列及び配列ヘルパー非推奨

use Illuminate\Support\Str;

Str クラスを use して使いましょう。

文字列をケバブケースにする

Str::kebab('fooBar'); // foo-bar

文字列をスネークケースにする

Str::snake('fooBar'); // foo_bar

文字列をキャメルケースに変換する

Str::camel('foo_bar'); // fooBar

キャメルノーテーションとも呼ぶ

文字列をパスカルケースに変換する

Str::studly('foo_bar'); // FooBar

アッパーキャメルケースとも呼ぶ

単数系の文字列を複数形に変換する

Str::plural('car'); // cars
Str::plural('child'); // children

複数形の文字列を単数系に変換する

Str::singular('cars'); // car
Str::singular('children'); // child

文字列をURLで使用可能な文字列に変換する

Str::slug('Laravel 5 Framework', '-'); // laravel-5-framework

補足: PHPの標準関数でできること

Laravelでも用意されてるものは合わせて紹介します。

文字列を小文字にする

strtolower('FOO_BAR'); // foo_bar
mb_strtolower('FOO_BAR'); // foo_bar

Str::lower('FOO_BAR'); // foo_bar

文字列を大文字にする

strtoupper('foo_bar'); // FOO_BAR
mb_strtoupper('foo_bar'); // FOO_BAR

Str::upper('foo_bar'); // FOO_BAR

文字列の最初の文字を大文字にする

ucfirst('foo bar'); // Foo bar

Str::ucfirst('foo bar'); // Foo bar

文字列の各単語の最初の文字を大文字にする

ucwords('foo bar'); // Foo Bar

Str::title('foo bar'); // Foo Bar

参考

36
24
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
36
24