Cakephp 2.X 文字列の変換ユーティリティ 複数形 単数形 大文字

今日はCakePHPのInflectorユーティリティのメモです。

これを使うと色々効率よく関数を書けるのでたまに使用しております。

[php]
$name = Inflector::pluralize($singular);
入力: Apple, Orange, Person, Man
出力: Apples, Oranges, People, Men
[/php]

[php]
$name = Inflector::singularize($plural)
入力: Apples, Oranges, People, Men
出力: Apple, Orange, Person, Man
[/php]

[php]
$name = Inflector::camelize($underscored)
入力: Apple_pie, some_thing, people_person
出力: ApplePie, SomeThing, PeoplePerson
[/php]

[php]
$name = Inflector::underscore($camelCase)
underscoreはキャメルケースの文字列をアンダースコア(_)に変換します。
入力: applePie, someThing
出力: apple_pie, some_thing
[/php]

[php]
$name = Inflector::humanize($underscored)
入力: apple_pie, some_thing, people_person
出力: Apple Pie, Some Thing, People Person
[/php]

[php]
$name = Inflector::tableize($camelCase)
入力: Apple, UserProfileSetting, Person
出力: apples, user_profile_settings, people
[/php]

[php]
$name = Inflector::classify($underscored)
入力: apples, user_profile_settings, people
出力: Apple, UserProfileSetting, Person
[/php]

[php]
$name = Inflector::variable($underscored)
入力: apples, user_result, people_people
出力: apples, userResult, peoplePeople
[/php]

[php]
$name = Inflector::slug($word, $replacement = ‘_’)
slugは特殊文字をラテン文字に変換したり、スペースをアンダースコアに変換します。slugはUTF-8を前提とします。
入力: apple purée
出力: apple_puree
[/php]

Follow me!