coduo / php-humanizer
将只对开发者可读的值人类化
5.0.0
2024-04-11 10:30 UTC
Requires
- php: ~8.1 || ~8.2 || ~8.3
- aeon-php/calendar: ^1.0
- symfony/translation: ~5.4||~6.4||~7
- symfony/yaml: ~5.4||~6.4||~7
Requires (Dev)
- thunderer/shortcode: ^0.7
Suggests
- ext-intl: Required if you are going to use humanizer with locales different than en_EN
README
将值人类化,使其对普通人可读 ;)
安装
运行以下命令
composer require coduo/php-humanizer
用法
文本
人类化
use Coduo\PHPHumanizer\StringHumanizer; StringHumanizer::humanize('field_name'); // "Field Name" StringHumanizer::humanize('user_id'); // "User" StringHumanizer::humanize('field_name', false); // "field name"
截断
截断字符串到最接近特定长度的单词
use Coduo\PHPHumanizer\StringHumanizer; $text = 'Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc.'; StringHumanizer::truncate($text, 8); // "Lorem ipsum" StringHumanizer::truncate($text, 8, '...'); // "Lorem ipsum..." StringHumanizer::truncate($text, 2); // "Lorem" StringHumanizer::truncate($text, strlen($text)); // "Lorem ipsum dolorem si amet, lorem ipsum. Dolorem sic et nunc."
截断HTML
截断HTML字符串到最接近特定长度的单词
use Coduo\PHPHumanizer\StringHumanizer; $text = '<p><b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup language</a> used to create <a href="/wiki/Web_page" title="Web page">web pages</a>.<sup id="cite_ref-1" class="reference"><a href="#cite_note-1"><span>[</span>1<span>]</span></a></sup> <a href="/wiki/Web_browser" title="Web browser">Web browsers</a> can read HTML files and render them into visible or audible web pages. HTML describes the structure of a <a href="/wiki/Website" title="Website">website</a> <a href="/wiki/Semantic" title="Semantic" class="mw-redirect">semantically</a> along with cues for presentation, making it a markup language, rather than a <a href="/wiki/Programming_language" title="Programming language">programming language</a>.</p>'; StringHumanizer::truncateHtml($text, 3); // "<b>HyperText</b>" StringHumanizer::truncateHtml($text, 12, ''); // "HyperText Markup" StringHumanizer::truncateHtml($text, 50, '', '...'); // "HyperText Markup Language, commonly referred to as..." StringHumanizer::truncateHtml($text, 75, '<b><i><u><em><strong><a><span>', '...'); // '<b>HyperText Markup Language</b>, commonly referred to as <b>HTML</b>, is the standard <a href="/wiki/Markup_language" title="Markup language">markup...</a>'
移除简码
$text = 'A text with [short]random[/short] [codes]words[/codes].'; StringHumanizer::removeShortcodes($text); // "A text with ." StringHumanizer::removeShortcodeTags($text); // "A text with random words."
数字
序数化
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::ordinalize(0); // "0th" NumberHumanizer::ordinalize(1); // "1st" NumberHumanizer::ordinalize(2); // "2nd" NumberHumanizer::ordinalize(23); // "23rd" NumberHumanizer::ordinalize(1002, 'nl'); // "1002e" NumberHumanizer::ordinalize(-111); // "-111th"
序数
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::ordinal(0); // "th" NumberHumanizer::ordinal(1); // "st" NumberHumanizer::ordinal(2); // "nd" NumberHumanizer::ordinal(23); // "rd" NumberHumanizer::ordinal(1002); // "nd" NumberHumanizer::ordinal(-111, 'nl'); // "e"
罗马数字
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::toRoman(1); // "I" NumberHumanizer::toRoman(5); // "V" NumberHumanizer::toRoman(1300); // "MCCC" NumberHumanizer::fromRoman("MMMCMXCIX"); // 3999 NumberHumanizer::fromRoman("V"); // 5 NumberHumanizer::fromRoman("CXXV"); // 125
二进制后缀
将字节数转换为最高适用的数据单位
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::binarySuffix(0); // "0 bytes" NumberHumanizer::binarySuffix(1); // "1 bytes" NumberHumanizer::binarySuffix(1024); // "1 kB" NumberHumanizer::binarySuffix(1025); // "1 kB" NumberHumanizer::binarySuffix(1536); // "1.5 kB" NumberHumanizer::binarySuffix(1048576 * 5); // "5 MB" NumberHumanizer::binarySuffix(1073741824 * 2); // "2 GB" NumberHumanizer::binarySuffix(1099511627776 * 3); // "3 TB" NumberHumanizer::binarySuffix(1325899906842624); // "1.18 PB"
数字也可以根据特定地区格式化
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::binarySuffix(1536, 'pl'); // "1,5 kB"
数字还可以使用特定的十进制位数通过preciseBinarySuffix($number, $precision, $locale = 'en')
进行人类化。精度参数必须在0到3之间。
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::preciseBinarySuffix(1024, 2); // "1.00 kB" NumberHumanizer::preciseBinarySuffix(1325899906842624, 3); // "1.178 PB"
此函数也支持地区
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::preciseBinarySuffix(1325899906842624, 3, 'pl'); // "1,178 PB"
计量后缀
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::metricSuffix(-1); // "-1" NumberHumanizer::metricSuffix(0); // "0" NumberHumanizer::metricSuffix(1); // "1" NumberHumanizer::metricSuffix(101); // "101" NumberHumanizer::metricSuffix(1000); // "1k" NumberHumanizer::metricSuffix(1240); // "1.2k" NumberHumanizer::metricSuffix(1240000); // "1.24M" NumberHumanizer::metricSuffix(3500000); // "3.5M"
数字也可以根据特定地区格式化
use Coduo\PHPHumanizer\NumberHumanizer; NumberHumanizer::metricSuffix(1240000, 'pl'); // "1,24M"
集合
牛津
use Coduo\PHPHumanizer\CollectionHumanizer; CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others" CollectionHumanizer::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other" CollectionHumanizer::oxford(['Michal', 'Norbert']); // "Michal and Norbert"
牛津使用翻译组件,因此您可以使用您喜欢的任何字符串格式。
日期时间
差异
use Coduo\PHPHumanizer\DateTimeHumanizer; DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:00")); // just now DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:00:05")); // 5 seconds from now DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:59:00")); // 1 minute ago DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:45:00")); // 15 minutes ago DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:15:00")); // 15 minutes from now DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 14:00:00")); // 1 hour from now DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 15:00:00")); // 2 hours from now DateTimeHumanizer::difference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:00:00")); // 1 hour ago DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-25")); // 1 day ago DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-24")); // 2 days ago DateTimeHumanizer::difference(new \DateTime("2014-04-26"), new \DateTime("2014-04-28")); // 2 days from now DateTimeHumanizer::difference(new \DateTime("2014-04-01"), new \DateTime("2014-04-15")); // 2 weeks from now DateTimeHumanizer::difference(new \DateTime("2014-04-15"), new \DateTime("2014-04-07")); // 1 week ago DateTimeHumanizer::difference(new \DateTime("2014-01-01"), new \DateTime("2014-04-01")); // 3 months from now DateTimeHumanizer::difference(new \DateTime("2014-05-01"), new \DateTime("2014-04-01")); // 1 month ago DateTimeHumanizer::difference(new \DateTime("2015-05-01"), new \DateTime("2014-04-01")); // 1 year ago DateTimeHumanizer::difference(new \DateTime("2014-05-01"), new \DateTime("2016-04-01")); // 2 years from now
精确差异
use Coduo\PHPHumanizer\DateTimeHumanizer; DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-25 11:20:00")); // 1 day, 1 hour, 40 minutes ago DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2015-04-28 17:00:00")); // 1 year, 2 days, 4 hours from now DateTimeHumanizer::preciseDifference(new \DateTime("2014-04-26 13:00:00"), new \DateTime("2016-04-27 13:00:00")); // 2 years, 1 day from now
艾翁日历
Aeon PHP 是一个面向日期和时间的库集合。
use Coduo\PHPHumanizer\DateTimeHumanizer; $timeUnit = TimeUnit::days(2) ->add(TimeUnit::hours(3)) ->add(TimeUnit::minutes(25)) ->add(TimeUnit::seconds(30)) ->add(TimeUnit::milliseconds(200)); DateTimeHumanizer::timeUnit($timeUnit); // 2 days, 3 hours, 25 minutes, and 30.2 seconds
目前我们支持以下语言
- 阿塞拜疆语
- 英语
- 波兰语
- 德语
- 土耳其语
- 法语
- 葡萄牙语 - 巴西
- 意大利语
- 荷兰语
- 俄语
- 挪威语
- 南非荷兰语
- 保加利亚语
- 印度尼西亚语
- 简体中文
- 繁体中文
- 西班牙语
- 乌克兰语
- 丹麦语
- 泰语
- 日语
- 罗马尼亚语
开发
下载库更新依赖关系后
composer update
为了检查依赖关系的最低可能版本,请添加
composer update --prefer-lowest
执行测试套件
composer run test
运行CS Fixer
composer run cs:php:fix
致谢
此库受到了Java Humanize Lib 和 Rails Active Support 的启发