waqarahmed/read-time
Composer 安装命令:
composer require waqarahmed/read-time
包简介
Get estimated read time of an article. Similar to medium.com's "x min read".
README 文档
README
Calculates the read time of an article.
Output string
e.g: x min read or 5 minutes read.
Features
- Multilingual translations support.
- Static to get simple abbreviated output string in English.
- Static method to get the integer value of the number of minutes required to read the given text.
- Multilingual and right-to-left language support.
- Support for Array output
- Support for JSON output
Installation
Installation using composer
composer require waqarahmed/read-time
Usage
Static Methods
There are two static methods minRead(string $text) andtime(sting $text).
minRead()
Use this method for a simple x min read message. It returns a rounded minutes number with a min read message.
$text = str_repeat('ad bc ', 251); //502 words in $text echo ReadTime::minRead($text);
The output:
2 min read
time()
time() method returns an array of the number of minutes and seconds required to read the given $text.
$text = str_repeat('ad bc ', 251); //502 words in $text ReadTime::time($text);
The output:
['minutes' => 2, 'seconds' => 12]
Class Methods
Create an instance of the class to use
- translation
- right-to-left language support
- JSON output
- array output
constructor()
The Constructor takes and sets these parameters:
public function __construct( string $text, array $translation = null, bool $abbreviate = true, bool $rtl = false, string $language = null, int $wordsPerMinute = 228 )
Class defaults
$wordsPerMinutedefault value is200$rtllanguage direction right-to-left isfalseby default$translationdefault isnullclass outputs the English language by default$abbreviateAbbreviate the word 'minute/minutes' to 'min' istrueby default
getTime()
After initiating a new class object, call the getTime() method to get the result.
Example:
4 minutes read or 1 minute read or abbreviated 4 min read.
setTextLanguge()
Reading time of different languages vary significantly (S. Klosinski, K. Dietz). Class method setTextLanguage() has estimated reading times of 17 languages taken from this study.
Reference: "Standardized Assessment of Reading Performance: The New International Reading Speed Texts IReST"
Language (iso-code) Words-per-minutes
Arabic (ar) 138, Chinese (zh) 158, Dutch (nl) 202, English (en) 228, Finnish (fi) 161, French (fr) 195, German (el) 179, Hebrew (he) 187, Italian (it) 188, Japanese (jp) 193, Polish (pl) 166, Portoguese (pt) 181, Russian (ru) 184, Slovenian (sl) 180, Spanish (es) 218, Swedish (sv) 199, Turkish (tr) 166.
English is the default language. Set different languages by passing two letters (ISO 639-1) language codes to the setTextLanguag() method.
An example: Setting Turkish as the input language.
$text = str_repeat('ad bc ', 251); //502 words in $text $result = new ReadTime($this->generateText(), ['minute' => 'dakika', 'minutes' => 'dakika', 'read' => 'okuman'], false, false, 'tr'); echo $result->getTime();
Translation
Pass translation array to the class to set the translations of the words: minute, minutes, min and read.
A passed array must be an associative array with any number of translation strings.
Default property of $translation
$translation = [ 'min' => 'min', 'minute' => 'minute', 'minutes' => 'minutes', 'read' => 'read', ];
Example translation input
$text = str_repeat('ad bc ', 251); //502 words in $text $result = new ReadTime($this->generateText(), ['minute' => 'minuto', 'minutes' => 'minutos', 'read' => 'leer'], false); echo $result->getTime();
The Spanish translated output: 2 minutos leer.
Right-to-Left Language Translation
Set $rtl property to true and pass $translation of languages written right-to-left.
$text = str_repeat('ad bc ', 251); $result = new ReadTime($this->generateText(), ['minute' => 'دقیقه', 'minutes' => 'دقایق', 'read' => 'خواندن'], false, true); echo $result->getTime();
Persian translated output: 'خواندن دقایق 2'
getJSON()
Method to get JSON output of claculated read time and class properties.
A class instance with default properties outputs:
$text = str_repeat('hello world ', 251); $result = new ReadTime($text); echo $result->getJSON();
outputs:
{ "minutes": 2, "time": { "minutes": 2, "seconds": 12 }, "wordCount": 502, "translation": { "min": "min", "minute": "minute", "minutes": "minutes", "read": "read" }, "abbreviate": true, "wordsPerMinute": 228 }
getArray()
Method to get array output of calculated read time and instance properties. A class instance with default properties:
$text = str_repeat('hello world ', 251); $result = new ReadTime($text); echo $result->getArray();
Outputs:
array(6) { ["minutes"]=> int(2) ["time"]=> array(2) { ["minutes"]=> int(2) ["seconds"]=> int(12) } ["wordCount"]=> int(502) ["translation"]=> array(4) { ["min"]=> string(3) "min" ["minute"]=> string(6) "minute" ["minutes"]=> string(7) "minutes" ["read"]=> string(4) "read" } ["abbreviate"]=> bool(true) ["wordsPerMinute"]=> int(228) }
waqarahmed/read-time 适用场景与选型建议
waqarahmed/read-time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 9, 最近一次更新时间为 2021 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 waqarahmed/read-time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waqarahmed/read-time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-23