gevman/inflector

Inflector 将英语名词进行复数化和单数化。它还包含一些其他有用的方法。

1.0 2018-02-19 09:52 UTC

This package is auto-updated.

Last update: 2024-09-21 02:41:56 UTC


README

string Inflector::pluralize(string $word)

将单词转换为复数形式。注意,这仅适用于英语!

例如,'apple' 将变为 'apples','child' 将变为 'children'。 -param string $word 要复数化的单词

string Inflector::singularize(string $word)

返回 $word 的单数形式。

  • param string $word 要单数化的英语单词
string Inflector::titleize(string $word [, bool $ucAll = false])

将下划线或驼峰式单词转换为英语句子。

  • param string $words
  • param $ucAll 是否将所有单词设置为 uppercase
string Inflector::camelize(string $word)

将 "send_email" 等单词转换为 "SendEmail"。它将从单词中删除非数字字符,因此 "who's online" 将转换为 "WhoSOnline"。

  • param string $word 要转换为 CamelCase 的单词
string Inflector::camel2words(string $word [, bool $ucwords = false])

将 CamelCase 名称转换为空格分隔的单词。

例如,'PostTag' 将转换为 'Post Tag'。

  • param string $name 要转换的字符串
  • param bool $ucwords 是否要大写每个单词的首字母
  • param $ucwords 是否要大写每个单词的首字母。
string Inflector::camel2id(string $name [, string $separator = '-' [, bool $strict = false]])

将 CamelCase 名称转换为小写 ID。ID 中的单词可以使用指定的字符连接(默认为 '-')。

例如,'PostTag' 将转换为 'post-tag'。

  • param string $name 要转换的字符串
  • param string $separator 用于连接 ID 中单词的字符
  • param bool|string $strict 是否在两个连续大写字母之间插入分隔符,默认为 false
string Inflector::id2camel(string $id [, string $separator = '-'])

将 ID 转换为 CamelCase 名称。ID 中的单词通过 $separator(默认为 '-')分隔,然后连接为一个 CamelCase 名称。

例如,'post-tag' 转换为 'PostTag'。

  • param string $id 要转换的 ID
  • param string $separator 用于分隔 ID 中单词的字符
string Inflector::underscore(string $words)

将任何 "CamelCased" 转换为 "underscored_word"。

  • param string $words 要下划线的单词
string Inflector::humanize(string $word [, bool $ucAll = false])

从 $word 返回一个可读的字符串。

  • param string $word 要 humanize 的字符串
  • param bool $ucAll 是否将所有单词设置为 uppercase 或不设置为 uppercase
string Inflector::variablize(string $word)

与 camelize 相同,但第一个字符是小写的。将 "send_email" 等单词转换为 "sendEmail"。它将从单词中删除非数字字符,因此 "who's online" 将转换为 "whoSOnline"。

  • param string $word 要转换为 lowerCamelCase
string Inflector::tableize(string $className)

将类名转换为相应的表名(复数形式)命名约定。

例如,将 "Person" 转换为 "people"。

  • param string $className 获取相关表名时使用的类名
string Inflector::slug(string $name [, string $replacement = '-' [, bool $lowercase = true]])

将所有空格转换为给定的替换项,删除非单词字符,并对其余字符进行转写。

如果 intl 扩展不可用,则使用后备方案,仅转换拉丁字符并删除其余字符。您可以通过助手的帮助程序 $transliteration 属性自定义字符映射。

  • param string $string 要转换的任意字符串
  • 参数字符串 $replacement 用于空格的替换
  • 参数布尔值 $lowercase 是否返回小写的字符串。默认值为 true
字符串 Inflector::transliterate(string $string [, string|\Transliterator $transliterator = null])

返回字符串的转写版本。

如果 intl 扩展不可用,则使用后备方案,仅转换拉丁字符并删除其余字符。您可以通过助手的帮助程序 $transliteration 属性自定义字符映射。

  • 参数字符串 $string 输入字符串
  • 参数字符串|\Transliterator $transliterator 可以是一个 [[\Transliterator]] 对象,或者是一个字符串,可以从该字符串构建一个 [[\Transliterator]] 对象。
字符串 Inflector::classify(string $tableName)

将表名转换为对应的类名。

例如,将 "people" 转换为 "Person"。

  • 参数字符串 $tableName
字符串 Inflector::ordinalize(int $number)

将数字转换为它的序数英语形式。例如,将 13 转换为 13th,2 转换为 2nd ...

  • 参数整数 $number 要获取其序数值的数字
字符串 Inflector::sentence(array $words [, $twoWordsConnector = ' and ' [, $lastWordConnector = null [, $connector = ', ']]])

将单词列表转换为句子。

对最后几个单词进行特殊处理。例如,

$words = ['Spain', 'France'];
echo Inflector::sentence($words);
// output: Spain and France

$words = ['Spain', 'France', 'Italy'];
echo Inflector::sentence($words);
// output: Spain, France and Italy

$words = ['Spain', 'France', 'Italy'];
echo Inflector::sentence($words, ' & ');
// output: Spain, France & Italy
  • 参数数组 $words 要转换成字符串的单词
  • 参数字符串 $twoWordsConnector 当只有两个单词时连接单词的字符串
  • 参数字符串 $lastWordConnector 连接最后两个单词的字符串。如果此参数为 null,则取 $twoWordsConnector 的值。
  • 参数字符串 $connector 除了由 $lastWordConnector 和 $twoWordsConnector 连接的单词之外连接单词的字符串