類別 yii\helpers\BaseInflector
繼承關係 | yii\helpers\BaseInflector |
---|---|
子類別 | yii\helpers\Inflector |
自版本 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseInflector.php |
BaseInflector 為 yii\helpers\Inflector 提供了具體的實作。
請勿使用 BaseInflector。請改用 yii\helpers\Inflector。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$plurals | array | 將單字轉換為複數形式的規則。 | yii\helpers\BaseInflector |
$singulars | array | 將單字轉換為單數形式的規則。 | yii\helpers\BaseInflector |
$specials | array | 在單字複數形式和單數形式之間轉換的特殊規則。 | yii\helpers\BaseInflector |
$transliteration | array | 當 intl 不可用時,transliterate() 使用的音譯回退對應表。 | yii\helpers\BaseInflector |
$transliterator | mixed | 可以是 Transliterator,或可用於建立 Transliterator 以進行音譯的字串。 | yii\helpers\BaseInflector |
公開方法
常數
常數 | 值 | 描述 | 定義於 |
---|---|---|---|
TRANSLITERATE_LOOSE | 'Any-Latin; Latin-ASCII; [\u0080-\uffff] remove' | Any-Latin; Latin-ASCII; [\u0080-\uffff] remove 音譯規則的快捷方式。此規則較為寬鬆,字母將使用基本拉丁 Unicode 區塊的字元進行音譯。例如:获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? 將音譯為 huo qu dao dochira Ukrainska: g,e, Srpska: d, n, d! Espanol? 。用於 transliterate()。如需詳細資訊,請參閱 unicode normalization forms |
yii\helpers\BaseInflector |
TRANSLITERATE_MEDIUM | 'Any-Latin; Latin-ASCII' | Any-Latin; Latin-ASCII 音譯規則的快捷方式。此規則為中等程度,字母將音譯為 Latin-1 (ISO 8859-1) ASCII 表格的字元。例如:获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? 將音譯為 huo qu dao dochira Ukrainsʹka: g,e, Srpska: d, n, d! ¿Espanol? 。用於 transliterate()。如需詳細資訊,請參閱 unicode normalization forms |
yii\helpers\BaseInflector |
TRANSLITERATE_STRICT | 'Any-Latin; NFKD' | Any-Latin; NFKD 音譯規則的快捷方式。此規則較為嚴格,字母將使用最接近的發音表示字元進行音譯。結果可能包含任何 UTF-8 字元。例如:获取到 どちら Українська: ґ,є, Српска: ђ, њ, џ! ¿Español? 將音譯為 huò qǔ dào dochira Ukraí̈nsʹka: g̀,ê, Srpska: đ, n̂, d̂! ¿Español? 。用於 transliterate()。如需詳細資訊,請參閱 unicode normalization forms |
yii\helpers\BaseInflector |
屬性詳細資料
將單字轉換為複數形式的規則。鍵為正則表達式,值為對應的替換字串。
'/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media)$/i' => '\1',
'/^(sea[- ]bass)$/i' => '\1',
'/(m)ove$/i' => '\1oves',
'/(f)oot$/i' => '\1eet',
'/(h)uman$/i' => '\1umans',
'/(s)tatus$/i' => '\1tatuses',
'/(s)taff$/i' => '\1taff',
'/(t)ooth$/i' => '\1eeth',
'/(quiz)$/i' => '\1zes',
'/^(ox)$/i' => '\1\2en',
'/([m|l])ouse$/i' => '\1ice',
'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
'/(x|ch|ss|sh)$/i' => '\1es',
'/([^aeiouy]|qu)y$/i' => '\1ies',
'/(hive)$/i' => '\1s',
'/(?:([^f])fe|([lr])f)$/i' => '\1\2ves',
'/sis$/i' => 'ses',
'/([ti])um$/i' => '\1a',
'/(p)erson$/i' => '\1eople',
'/(m)an$/i' => '\1en',
'/(c)hild$/i' => '\1hildren',
'/(buffal|tomat|potat|ech|her|vet)o$/i' => '\1oes',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
'/us$/i' => 'uses',
'/(alias)$/i' => '\1es',
'/(ax|cris|test)is$/i' => '\1es',
'/(currenc)y$/' => '\1ies',
'/s$/' => 's',
'/^$/' => '',
'/$/' => 's',
]
將單字轉換為單數形式的規則。鍵為正則表達式,值為對應的替換字串。
'/([nrlm]ese|deer|fish|sheep|measles|ois|pox|media|ss)$/i' => '\1',
'/^(sea[- ]bass)$/i' => '\1',
'/(s)tatuses$/i' => '\1tatus',
'/(f)eet$/i' => '\1oot',
'/(t)eeth$/i' => '\1ooth',
'/^(.*)(menu)s$/i' => '\1\2',
'/(quiz)zes$/i' => '\1',
'/(matr)ices$/i' => '\1ix',
'/(vert|ind)ices$/i' => '\1ex',
'/^(ox)en/i' => '\1',
'/(alias)(es)*$/i' => '\1',
'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
'/([ftw]ax)es/i' => '\1',
'/(cris|ax|test)es$/i' => '\1is',
'/(shoe|slave)s$/i' => '\1',
'/(o)es$/i' => '\1',
'/ouses$/' => 'ouse',
'/([^a])uses$/' => '\1us',
'/([m|l])ice$/i' => '\1ouse',
'/(x|ch|ss|sh)es$/i' => '\1',
'/(m)ovies$/i' => '\1\2ovie',
'/(s)eries$/i' => '\1\2eries',
'/([^aeiouy]|qu)ies$/i' => '\1y',
'/([lr])ves$/i' => '\1f',
'/(tive)s$/i' => '\1',
'/(hive)s$/i' => '\1',
'/(drive)s$/i' => '\1',
'/([^fo])ves$/i' => '\1fe',
'/(^analy)ses$/i' => '\1sis',
'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
'/([ti])a$/i' => '\1um',
'/(p)eople$/i' => '\1\2erson',
'/(m)en$/i' => '\1an',
'/(c)hildren$/i' => '\1\2hild',
'/(n)ews$/i' => '\1\2ews',
'/(n)etherlands$/i' => '\1\2etherlands',
'/eaus$/' => 'eau',
'/(currenc)ies$/' => '\1y',
'/^(.*us)$/' => '\1',
'/s$/i' => '',
]
在單字複數形式和單數形式之間轉換的特殊規則。鍵為單數形式的特殊單字,值為對應的複數形式。
'atlas' => 'atlases',
'beef' => 'beefs',
'brother' => 'brothers',
'cafe' => 'cafes',
'child' => 'children',
'cookie' => 'cookies',
'corpus' => 'corpuses',
'cow' => 'cows',
'curve' => 'curves',
'foe' => 'foes',
'ganglion' => 'ganglions',
'genie' => 'genies',
'genus' => 'genera',
'graffito' => 'graffiti',
'hoof' => 'hoofs',
'loaf' => 'loaves',
'man' => 'men',
'money' => 'monies',
'mongoose' => 'mongooses',
'move' => 'moves',
'mythos' => 'mythoi',
'niche' => 'niches',
'numen' => 'numina',
'occiput' => 'occiputs',
'octopus' => 'octopuses',
'opus' => 'opuses',
'ox' => 'oxen',
'pasta' => 'pasta',
'penis' => 'penises',
'sex' => 'sexes',
'soliloquy' => 'soliloquies',
'testis' => 'testes',
'trilby' => 'trilbys',
'turf' => 'turfs',
'wave' => 'waves',
'Amoyese' => 'Amoyese',
'bison' => 'bison',
'Borghese' => 'Borghese',
'bream' => 'bream',
'breeches' => 'breeches',
'britches' => 'britches',
'buffalo' => 'buffalo',
'cantus' => 'cantus',
'carp' => 'carp',
'chassis' => 'chassis',
'clippers' => 'clippers',
'cod' => 'cod',
'coitus' => 'coitus',
'Congoese' => 'Congoese',
'contretemps' => 'contretemps',
'corps' => 'corps',
'debris' => 'debris',
'diabetes' => 'diabetes',
'djinn' => 'djinn',
'eland' => 'eland',
'elk' => 'elk',
'equipment' => 'equipment',
'Faroese' => 'Faroese',
'flounder' => 'flounder',
'Foochowese' => 'Foochowese',
'gallows' => 'gallows',
'Genevese' => 'Genevese',
'Genoese' => 'Genoese',
'Gilbertese' => 'Gilbertese',
'graffiti' => 'graffiti',
'headquarters' => 'headquarters',
'herpes' => 'herpes',
'hijinks' => 'hijinks',
'Hottentotese' => 'Hottentotese',
'information' => 'information',
'innings' => 'innings',
'jackanapes' => 'jackanapes',
'Kiplingese' => 'Kiplingese',
'Kongoese' => 'Kongoese',
'Lucchese' => 'Lucchese',
'mackerel' => 'mackerel',
'Maltese' => 'Maltese',
'mews' => 'mews',
'moose' => 'moose',
'mumps' => 'mumps',
'Nankingese' => 'Nankingese',
'news' => 'news',
'nexus' => 'nexus',
'Niasese' => 'Niasese',
'Pekingese' => 'Pekingese',
'Piedmontese' => 'Piedmontese',
'pincers' => 'pincers',
'Pistoiese' => 'Pistoiese',
'pliers' => 'pliers',
'Portuguese' => 'Portuguese',
'proceedings' => 'proceedings',
'rabies' => 'rabies',
'rice' => 'rice',
'rhinoceros' => 'rhinoceros',
'salmon' => 'salmon',
'Sarawakese' => 'Sarawakese',
'scissors' => 'scissors',
'series' => 'series',
'Shavese' => 'Shavese',
'shears' => 'shears',
'siemens' => 'siemens',
'species' => 'species',
'swine' => 'swine',
'testes' => 'testes',
'trousers' => 'trousers',
'trout' => 'trout',
'tuna' => 'tuna',
'Vermontese' => 'Vermontese',
'Wenchowese' => 'Wenchowese',
'whiting' => 'whiting',
'wildebeest' => 'wildebeest',
'Yengeese' => 'Yengeese',
'software' => 'software',
'hardware' => 'hardware',
]
當 intl 不可用時,transliterate() 使用的音譯回退對應表。
'À' => 'A',
'Á' => 'A',
'Â' => 'A',
'Ã' => 'A',
'Ä' => 'A',
'Å' => 'A',
'Æ' => 'AE',
'Ç' => 'C',
'È' => 'E',
'É' => 'E',
'Ê' => 'E',
'Ë' => 'E',
'Ì' => 'I',
'Í' => 'I',
'Î' => 'I',
'Ï' => 'I',
'Ð' => 'D',
'Ñ' => 'N',
'Ò' => 'O',
'Ó' => 'O',
'Ô' => 'O',
'Õ' => 'O',
'Ö' => 'O',
'Ő' => 'O',
'Ø' => 'O',
'Ù' => 'U',
'Ú' => 'U',
'Û' => 'U',
'Ü' => 'U',
'Ű' => 'U',
'Ý' => 'Y',
'Þ' => 'TH',
'ß' => 'ss',
'à' => 'a',
'á' => 'a',
'â' => 'a',
'ã' => 'a',
'ä' => 'a',
'å' => 'a',
'æ' => 'ae',
'ç' => 'c',
'è' => 'e',
'é' => 'e',
'ê' => 'e',
'ë' => 'e',
'ì' => 'i',
'í' => 'i',
'î' => 'i',
'ï' => 'i',
'ð' => 'd',
'ñ' => 'n',
'ò' => 'o',
'ó' => 'o',
'ô' => 'o',
'õ' => 'o',
'ö' => 'o',
'ő' => 'o',
'ø' => 'o',
'ù' => 'u',
'ú' => 'u',
'û' => 'u',
'ü' => 'u',
'ű' => 'u',
'ý' => 'y',
'þ' => 'th',
'ÿ' => 'y',
]
可以是 Transliterator,或是一個字串,由此字串可以建立一個 Transliterator 用於音譯。當 intl 擴充可用時,會被 transliterate() 使用。預設為 TRANSLITERATE_LOOSE
另請參閱 https://php.dev.org.tw/manual/en/transliterator.transliterate.php。
Method Details
將 CamelCase 名稱轉換為小寫的 ID。
ID 中的單字可以使用指定的字元串連(預設為 '-')。例如,'PostTag' 將會被轉換為 'post-tag'。
public static string camel2id ( $name, $separator = '-', $strict = false ) | ||
$name | string |
要轉換的字串 |
$separator | string |
用於串連 ID 中單字的字元 |
$strict | boolean|string |
是否在兩個連續的大寫字元之間插入分隔符號,預設為 false |
return | string |
產生的 ID |
---|
public static function camel2id($name, $separator = '-', $strict = false)
{
if (empty($name)) {
return (string) $name;
}
$regex = $strict ? '/\p{Lu}/u' : '/(?<!\p{Lu})\p{Lu}/u';
if ($separator === '_') {
return mb_strtolower(trim(preg_replace($regex, '_\0', $name), '_'), self::encoding());
}
return mb_strtolower(trim(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name)), $separator), self::encoding());
}
將 CamelCase 名稱轉換為以空格分隔的單字。
例如,'PostTag' 將會被轉換為 'Post Tag'。
public static string camel2words ( $name, $ucwords = true ) | ||
$name | string |
要轉換的字串 |
$ucwords | boolean |
是否將每個單字的首字母大寫 |
return | string |
產生的單字 |
---|
public static function camel2words($name, $ucwords = true)
{
if (empty($name)) {
return (string) $name;
}
// Add a space before any uppercase letter preceded by a lowercase letter (xY => x Y)
// and any uppercase letter preceded by an uppercase letter and followed by a lowercase letter (XYz => X Yz)
$label = preg_replace('/(?<=\p{Ll})\p{Lu}|(?<=\p{L})\p{Lu}(?=\p{Ll})/u', ' \0', $name);
$label = mb_strtolower(trim(str_replace(['-', '_', '.'], ' ', $label)), self::encoding());
return $ucwords ? StringHelper::mb_ucwords($label, self::encoding()) : $label;
}
以 CamelCase 形式傳回給定的單字。
將像 "send_email" 這樣的單字轉換為 "SendEmail"。它會移除單字中非字母數字的字元,所以 "who's online" 將會被轉換為 "WhoSOnline"。
另請參閱 variablize()。
public static string camelize ( $word ) | ||
$word | string |
要轉換為 CamelCase 的單字 |
public static function camelize($word)
{
if (empty($word)) {
return (string) $word;
}
return str_replace(' ', '', StringHelper::mb_ucwords(preg_replace('/[^\pL\pN]+/u', ' ', $word), self::encoding()));
}
將資料表名稱轉換為其類別名稱。
例如,將 "people" 轉換為 "Person"。
public static string classify ( $tableName ) | ||
$tableName | string |
public static function classify($tableName)
{
if (empty($tableName)) {
return (string) $tableName;
}
return static::camelize(static::singularize($tableName));
}
protected static boolean hasIntl ( ) | ||
return | boolean |
如果 intl 擴充已載入 |
---|
protected static function hasIntl()
{
return extension_loaded('intl');
}
從 $word 傳回人類可讀的字串。
public static string humanize ( $word, $ucAll = false ) | ||
$word | string |
要人性化的字串 |
$ucAll | boolean |
是否將所有單字設為大寫 |
public static function humanize($word, $ucAll = false)
{
if (empty($word)) {
return (string) $word;
}
$word = str_replace('_', ' ', preg_replace('/_id$/', '', $word));
$encoding = self::encoding();
return $ucAll ? StringHelper::mb_ucwords($word, $encoding) : StringHelper::mb_ucfirst($word, $encoding);
}
將 ID 轉換為 CamelCase 名稱。
ID 中以 $separator
分隔的單字(預設為 '-')將會被串連成一個 CamelCase 名稱。例如,'post-tag' 會被轉換為 'PostTag'。
public static string id2camel ( $id, $separator = '-' ) | ||
$id | string |
要轉換的 ID |
$separator | string |
用於分隔 ID 中單字的字元 |
return | string |
產生的 CamelCase 名稱 |
---|
public static function id2camel($id, $separator = '-')
{
if (empty($id)) {
return (string) $id;
}
return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
}
將數字轉換為其英文序數形式。例如,將 13 轉換為 13th,2 轉換為 2nd。
..
public static string ordinalize ( $number ) | ||
$number | 整數 |
取得序數值的數字 |
public static function ordinalize($number)
{
if (in_array($number % 100, range(11, 13))) {
return $number . 'th';
}
switch ($number % 10) {
case 1:
return $number . 'st';
case 2:
return $number . 'nd';
case 3:
return $number . 'rd';
default:
return $number . 'th';
}
}
將單字轉換為複數形式。
請注意,這僅適用於英文!例如,'apple' 將會變成 'apples',而 'child' 將會變成 'children'。
public static string pluralize ( $word ) | ||
$word | string |
要轉換為複數的單字 |
return | string |
複數化的單字 |
---|
public static function pluralize($word)
{
if (empty($word)) {
return (string) $word;
}
if (isset(static::$specials[$word])) {
return static::$specials[$word];
}
foreach (static::$plurals as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
將單字列表轉換為句子。
最後幾個單字會進行特殊處理。例如,
$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
public static string sentence ( array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ' ) | ||
$words | array |
要轉換成字串的單字 |
$twoWordsConnector | string|null |
當只有兩個單字時,連接單字的字串 |
$lastWordConnector | string|null |
連接最後兩個單字的字串。如果這是 null,它將採用 |
$connector | string |
連接單字的字串,但排除由 $lastWordConnector 和 $twoWordsConnector 連接的單字 |
return | string |
產生的句子 |
---|
public static function sentence(array $words, $twoWordsConnector = null, $lastWordConnector = null, $connector = ', ')
{
if ($twoWordsConnector === null) {
$twoWordsConnector = Yii::t('yii', ' and ');
}
if ($lastWordConnector === null) {
$lastWordConnector = $twoWordsConnector;
}
switch (count($words)) {
case 0:
return '';
case 1:
return reset($words);
case 2:
return implode($twoWordsConnector, $words);
default:
return implode($connector, array_slice($words, 0, -1)) . $lastWordConnector . end($words);
}
}
傳回 $word 的單數形式。
public static string singularize ( $word ) | ||
$word | string |
要轉換為單數的英文單字 |
return | string |
單數名詞。 |
---|
public static function singularize($word)
{
if (empty($word)) {
return (string) $word;
}
$result = array_search($word, static::$specials, true);
if ($result !== false) {
return $result;
}
foreach (static::$singulars as $rule => $replacement) {
if (preg_match($rule, $word)) {
return preg_replace($rule, $replacement, $word);
}
}
return $word;
}
傳回一個字串,其中所有空格都轉換為給定的替換字元,非單字字元被移除,其餘字元則進行音譯。
如果 intl 擴充套件不可用,則使用僅轉換拉丁字符並移除其餘字符的回退機制。您可以透過 helper 的 $transliteration 屬性自訂字符映射。
public static string slug ( $string, $replacement = '-', $lowercase = true ) | ||
$string | string |
要轉換的任意字串 |
$replacement | string |
用於空格的替換字元 |
$lowercase | boolean |
是否以小寫形式傳回字串。預設值為 |
return | string |
轉換後的字串。 |
---|
public static function slug($string, $replacement = '-', $lowercase = true)
{
if (empty($string)) {
return (string) $string;
}
if ((string)$replacement !== '') {
$parts = explode($replacement, static::transliterate($string));
} else {
$parts = [static::transliterate($string)];
}
$replaced = array_map(function ($element) use ($replacement) {
$element = preg_replace('/[^a-zA-Z0-9=\s—–-]+/u', '', $element);
return preg_replace('/[=\s—–-]+/u', $replacement, $element);
}, $parts);
$string = trim(implode($replacement, $replaced), $replacement);
if ((string)$replacement !== '') {
$string = preg_replace('#' . preg_quote($replacement, '#') . '+#', $replacement, $string);
}
return $lowercase ? strtolower($string) : $string;
}
將類別名稱轉換為其資料表名稱(複數形式)命名慣例。
例如,將 "Person" 轉換為 "people"。
public static string tableize ( $className ) | ||
$className | string |
用於取得相關 table_name 的類別名稱 |
public static function tableize($className)
{
if (empty($className)) {
return (string) $className;
}
return static::pluralize(static::underscore($className));
}
將底線或 CamelCase 單字轉換為英文句子。
public static string titleize ( $words, $ucAll = false ) | ||
$words | string | |
$ucAll | boolean |
是否將所有單字設為大寫 |
public static function titleize($words, $ucAll = false)
{
if (empty($words)) {
return (string) $words;
}
$words = static::humanize(static::underscore($words), $ucAll);
return $ucAll ? StringHelper::mb_ucwords($words, self::encoding()) : StringHelper::mb_ucfirst($words, self::encoding());
}
傳回字串的音譯版本。
如果 intl 擴充套件不可用,則使用僅轉換拉丁字符並移除其餘字符的回退機制。您可以透過 helper 的 $transliteration 屬性自訂字符映射。
public static string transliterate ( $string, $transliterator = null ) | ||
$string | string |
輸入字串 |
$transliterator | string|Transliterator|null |
可以是 Transliterator 或可用於建構 Transliterator 的字串。 |
版本 | 描述 |
---|---|
2.0.7 | 這個方法是公開的。 |
public static function transliterate($string, $transliterator = null)
{
if (empty($string)) {
return (string) $string;
}
if (static::hasIntl()) {
if ($transliterator === null) {
$transliterator = static::$transliterator;
}
return transliterator_transliterate($transliterator, $string);
}
return strtr($string, static::$transliteration);
}
將任何 "CamelCased" 轉換為 "underscored_word"。
public static string underscore ( $words ) | ||
$words | string |
要轉換為底線的單字 |
public static function underscore($words)
{
if (empty($words)) {
return (string) $words;
}
return mb_strtolower(preg_replace('/(?<=\\pL)(\\p{Lu})/u', '_\\1', $words), self::encoding());
}
與 camelize 相同,但第一個字元為小寫。
將類似 "send_email" 的單字轉換為 "sendEmail"。它會移除單字中非字母數字的字元,因此 "who's online" 將會轉換為 "whoSOnline"。
public static string variablize ( $word ) | ||
$word | string |
轉換為 lowerCamelCase |
public static function variablize($word)
{
if (empty($word)) {
return (string) $word;
}
$word = static::camelize($word);
return mb_strtolower(mb_substr($word, 0, 1, self::encoding())) . mb_substr($word, 1, null, self::encoding());
}
註冊 或 登入 以便發表評論。