icm-services / tetra-text
Regulus TetraText for Laravel 6 的分支,用于格式化数值、货币、电话号码等。
Requires
- php: >=5.4.0
- ezyang/htmlpurifier: >=4.8.0
- laravel/framework: 5.*
README
一个用于 Laravel 5 的小型文本/字符串格式化 composer 包,可格式化数值、货币、电话号码等。还提供了一些有限的日期函数。
安装
要安装 TetraText,请确保 "regulus/tetra-text" 已添加到 Laravel 5 的 composer.json
文件中。
"require": {
"regulus/tetra-text": "0.6.*"
},
然后从命令行运行 php composer.phar update
。Composer 将安装 TetraText 包。现在,您只需注册服务提供者并在 config/app.php
中设置 TetraText 的别名。将以下内容添加到 providers
数组
Regulus\TetraText\TetraTextServiceProvider::class,
并添加以下内容到 aliases
数组
'Format' => Regulus\TetraText\Facade::class,
您可以使用 'TetraText' 或其他别名,但出于简单起见,建议使用 'Format'。现在 TetraText 已准备好使用。
可用函数
将字符串格式化为数字
// note: numeric() is an alias for Format::numeric() // format a string as numeric echo numeric($value); // disallow decimals echo numeric($value, false); // allow negative numbers echo numeric($value, true, true);
将字符串格式化为美元值
// note: money() is an alias for Format::money() // format a string as money echo money(343); // use euro symbol instead of dollar symbol echo money(343, '€'); // disallow negative values (will output "$0.00") echo money(-343, '$', false); // remove thousands separator echo money(343, '$', true, '');
money()
函数与 PHP 的 number_format()
的优势在于,负美元值将输出为 -$343.00
,而不是简单地使用美元符号连接到使用 number_format()
格式化的字符串时出现的 $-343.00
。
将值转换为指定总数的百分比(并避免“除以零”错误)
// note: percent() is an alias for Format::percent() // will output "25%" echo percent(25, 100); // will output "0%" echo percent(25, 0); // use zero decimal places (default is 1) echo percent(25, 100, 0); // return as a number rather than a string with the "%" symbol concatenated to the end echo percent(25, 100, 0, true);
格式化北美电话号码
// note: phone() is an alias for Format::phone() // will output "(403) 343-1123" echo phone(14033431123); // will output "1 (403) 343-1123" echo phone(14033431123, ['digits' => 11]); // will output "(403) 343.1123" echo phone('1-403-343-1123', ['separator' => '.']); // will output "403.343.1123" echo phone('1-403-343-1123', ['separator' => '.', 'areaCodeBrackets' => false]); // will output "(403) 343-1123 x343" echo phone('1-403-343-1123 Ext. 343'); // will output "(403) 343-1123 Ext. 343" echo phone('1-403-343-1123 x343', ['extensionSeparator' => ' Ext. ']); // will output "(403) 343-1123" echo phone('1-403-343-1123 Ext. 343', ['stripExtension' => true]);
您可以将字符串或整数传递给 phone()
函数。在格式化变量为电话号码之前,它将自动删除非数字字符。
格式化加拿大邮政编码
// note: postal_code() is an alias for Format::postalCode() // will output "S0N 0H0" echo postal_code('s0n0h0'); // will output "S0N0H0" echo postal_code('s0n0h0', false);
将布尔值格式化为字符串
// note: bool_to_str() is an alias for Format::boolToStr() // will output "Yes" echo bool_to_str(true); // will output "No" echo bool_to_str(false); // will output "Off" echo bool_to_str(false, 'On/Off'); // will output "Up" echo bool_to_str(true, ['Up', 'Down']);
给数字添加后缀
// will output '1<sup class="number-suffix">st</sup>' echo Format::numberSuffix(1); // will output "2nd" echo Format::numberSuffix(2, false); // will output "3<span>rd</span>" echo Format::numberSuffix(3, 'span', false);
根据指定数字复数化项目名称
// will output "item" echo Format::pluralize('item', 1); // will output "items" echo Format::pluralize('item', 2); // will output "fungi" echo Format::pluralize('fungus', 2, 'fungi');
根据指定数字复数化字符串
$users = User::all(); $message = "Displaying :number :item."; // may output "Displaying 3 users." echo Format::pluralizeMessage($message, 'user', count($users));
获取项目名称的正确英文前缀(“a”或“an”,根据起始音节的发音)
// will output "a frog" echo Format::a('frog'); // will output "an octopus" echo Format::a('octopus'); // will output "an HTML" (method checks to see if item is all uppercase to denote acronym and then uses letter sound instead) echo Format::a('HTML');
将字符串转换为 HTML 字符
// will output "Penn & Teller" echo entities('Penn & Teller');
将字符串转换为 URI 段落
// will output "turn-this-title-into-a-slug" echo Format::slug('Turn This Title Into a Slug!');
根据指定的表名和字段名转换字符串为唯一的 URI 段落
// may output "turn-this-title-into-a-slug-2" if "blog_posts" table already has a row with slug echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts'); // set an ID to ignore/prevent slug conflicts for (ID of table row being edited) echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts', ['ignoreId' => 343]); // set a character limit for the slug echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts', ['charLimit' => 64]); // use a different field than "slug" in DB table echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts', ['field' => 'uri_tag']); // ignore soft deleted records echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts', ['softDelete' => true]); // add additional matching values echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts', ['matchingValues' => ['type' => 'Microblog']]); echo Format::uniqueSlug('Turn This Title Into a Slug!', 'blog_posts', ['matchingValues' => ['type' => '>= 3']]);
获取一周的第一天
// will output "2013-09-22" (using "Sunday" as the first day) echo Format::firstDayOfWeek('2013-09-27'); // will output "2013-09-23" echo Format::firstDayOfWeek('2013-09-27', 'Monday');
获取一周的最后一天
// will output "2013-09-28" (using "Sunday" as the first day) echo Format::lastDayOfWeek('2013-09-27'); // will output "2013-09-29" echo Format::lastDayOfWeek('2013-09-27', 'Monday');
获取一个月的第一天
// will output "September 1" echo Format::firstDayOfMonth('2013-09-27', 'F j');
获取一个月的最后一天
// will output "2013-09-30" echo Format::lastDayOfMonth('2013-09-27');
将换行符转换为段落
// will output "<p>This is the first paragraph.</p><p>This is the second paragraph.</p>" echo Format::paragraphs("This is the first paragraph.\nThis is the second paragraph.");
对字符串应用字符限制
$string = 'I define <strong>anarchist society</strong> as one where there is no legal possibility for coercive aggression against the person or property of any individual. Anarchists oppose the State because it has its very being in such aggression, namely, the expropriation of private property through taxation, the coercive exclusion of other providers of defense service from its territory, and all of the other depredations and coercions that are built upon these twin foci of invasions of individual rights. <div class="author">-Murray Rothbard</div>'; // will output 'I define <strong>anarchist society</strong> as one where there is no legal possibility for coercive aggression<span class="exceeded-limit">...</span>' echo char_limit($string, 93); // will output 'I define <strong>anarchist society</strong> as one where there is no legal possibility for coercive aggression' echo char_limit($string, 93, ['exceededText' => false]); // will output 'I define <strong>anarchist society</strong> as one where there is no legal possibility for coercive aggression<a href="https://en.wikiquote.org/wiki/Murray_Rothbard" class="read-more">Read more...</a>' echo word_limit($string, 14, ['exceededText' => 'Read more...', 'exceededLinkUrl' => 'https://en.wikiquote.org/wiki/Murray_Rothbard']); // note: char_limit() and word_limit() are aliases for Format::charLimit(), Format::wordLimit()
注意:
charLimit()
和wordLimit()
被设计为保持 HTML 标签完整性。
对字符串应用字符限制
// get a translation and make it lowercase (if it does not appear to be an acronym) echo trans_l($value); // get a translation choice and make it lowercase (if it does not appear to be an acronym) echo trans_choice_l($value, 1); // will output "item" from translation variable of "Item|Items" // get a translation and prepend with "a" or "an" if language is English or exceeds 2 letter language code echo trans_a($value); // will output "an umbrella" from translation variable of "umbrella" // get a translation and prepend with "a" or "an" if language is English or exceeds 2 letter language code echo trans_choice_a($value, 1); // will output "an umbrella" from translation variable of "umbrella|umbrellas" echo trans_choice_a($value, 2); // will output "2 umbrellas" from translation variable of "umbrella|umbrellas" // note: trans_l(), trans_choice_l(), trans_a(), and trans_choice_a() are aliases // for Format::transL(), Format::transChoiceL(), Format::transA(), and Format::transChoiceA()
注意:
transA()
和transChoiceA()
也可以通过将第二个或第三个参数设置为true
来使用结果字符串的小写。第二个参数(对于transA()
是第二个,对于transChoiceA()
是第三个)是parameters
数组,但如果它是一个布尔值,则将parameters
数组设置为空,并将它解释为lower
参数。