類別 yii\helpers\Inflector
繼承 | yii\helpers\Inflector » yii\helpers\BaseInflector |
---|---|
起始版本 | 2.0 |
原始碼 | https://github.com/yiisoft/yii2/blob/master/framework/helpers/Inflector.php |
Inflector 可以將英文名詞轉換為複數和單數形式。它還包含其他一些有用的方法。
公開屬性
屬性 | 類型 | 描述 | 定義於 |
---|---|---|---|
$plurals | 陣列 | 將單字轉換為複數形式的規則。 | yii\helpers\BaseInflector |
$singulars | 陣列 | 將單字轉換為單數形式的規則。 | yii\helpers\BaseInflector |
$specials | 陣列 | 在單字複數形式和單數形式之間轉換的特殊規則。 | yii\helpers\BaseInflector |
$transliteration | 陣列 | 當 intl 不可用時,transliterate() 使用的音譯後備地圖。 | yii\helpers\BaseInflector |
$transliterator | 混合 | 可以是 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 |
方法詳情
定義於: yii\helpers\BaseInflector::camel2id()
將 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());
}
定義於: yii\helpers\BaseInflector::camel2words()
將 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;
}
定義於: yii\helpers\BaseInflector::camelize()
以 CamelCase 形式返回給定的單字。
將類似 "send_email" 的單字轉換為 "SendEmail"。它會移除單字中非英數字元的字元,所以 "who's online" 將會轉換為 "WhoSOnline"。
另請參閱 variablize()。
public static string camelize ( $word ) | ||
$word | string |
要轉換為駝峰式命名法的單字 |
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()));
}
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');
}
定義於: yii\helpers\BaseInflector::humanize()
從 $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);
}
定義於: yii\helpers\BaseInflector::id2camel()
將 ID 轉換為 CamelCase 名稱。
ID 中的單字以 $separator
(預設為 '-')分隔,將會串連成駝峰式命名。例如,'post-tag' 會轉換為 'PostTag'。
public static string id2camel ( $id, $separator = '-' ) | ||
$id | string |
要轉換的 ID |
$separator | string |
用於分隔 ID 中單字的字元 |
return | string |
轉換後的駝峰式命名 |
---|
public static function id2camel($id, $separator = '-')
{
if (empty($id)) {
return (string) $id;
}
return str_replace(' ', '', StringHelper::mb_ucwords(str_replace($separator, ' ', $id), self::encoding()));
}
public static string ordinalize ( $number ) | ||
$number | integer |
要取得序數值的數字 |
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';
}
}
定義於: yii\helpers\BaseInflector::pluralize()
將單字轉換為複數形式。
請注意,這僅適用於英文!例如,'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;
}
定義於: yii\helpers\BaseInflector::sentence()
將單字列表轉換為句子。
最後幾個單字會經過特殊處理。例如,
$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 | 陣列 |
要轉換成字串的單字陣列 |
$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);
}
}
定義於: yii\helpers\BaseInflector::singularize()
返回 $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;
}
定義於: yii\helpers\BaseInflector::slug()
返回一個字串,其中所有空格都轉換為給定的替換字元,非單字字元被移除,其餘字元被音譯。
如果 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;
}
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));
}
定義於: yii\helpers\BaseInflector::titleize()
將底線或 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());
}
定義於: yii\helpers\BaseInflector::transliterate()
返回字串的音譯版本。
如果 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);
}
定義於: yii\helpers\BaseInflector::underscore()
將任何 「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());
}
定義於: yii\helpers\BaseInflector::variablize()
與 camelize 相同,但第一個字元為小寫。
將類似 "send_email" 的單字轉換為 "sendEmail"。它會移除單字中非英數字元的字元,所以 "who's online" 將會轉換為 "whoSOnline"。
public static string variablize ( $word ) | ||
$word | string |
轉換為小駝峰式命名法 |
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());
}
請註冊或登入以發表評論。