realodix / readtime
Medium's like reading time estimation.
Installs: 12 936
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 1
Open Issues: 0
Requires
- php: ^8.1
- illuminate/collections: ^10.0 || ^11.0
- symfony/options-resolver: ^6.1 || ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.5 || ^11.0
- realodix/relax: ^1.7
README
📚 Medium's like reading time estimation, based on Medium's read time formula.
Sites like Medium.com have popularized the concept of giving users an estimate for the amount of time it will take them to read the content. With this convenience in mind, ReadTime gives PHP developers the same tool for their readable content. It's a simple feature that will give a nice touch to your PHP application.
Features
- Calculates read time of images in decreasing progression (Example - 12 seconds for the first image, 11 for the second, until images counted at 3 seconds).
- Calculates read time of the Chinese / Japanese / Korean characters separately.
- Removes unwanted html tags to calculate read time more efficiently.
Reference
- help.medium.com/articles/Read-time
- medium.com/blogging-guide/how-is-medium-article-read-time-calculated
Installation
You can install the package via composer:
composer require realodix/readtime
Quick start
Here is an example of the most basic usage:
<?php use Realodix\ReadTime\ReadTime; $readTime = new ReadTime('foo bar'); echo $readTime->get(); // less than a minute
You may also pass several arguments to the constructor if you wish to change settings on the fly:
/** * @param string|array $content The content to analyze * @param int $wpm Speed of reading the text in Words per Minute * @param int $imageTime Speed of reading the image in seconds * @param int $cpm Speed of reading the Chinese / Korean / Japanese * characters in Characters per Minute */ $readTime = new ReadTime($content, $wpm = 265, $imageTime = 12, $cpm = 500); echo $readTime->get();
The ReadTime class is able to accept a string of content or a array (flat or multidimensional) of multiple pieces of content. This may come in handy if you are attempting to display the total read time of body content along with sidebar content.
For example:
$readTime = new ReadTime([$content, $moreContent, $evenMoreContent]); echo $readTime->get();
❗ Named arguments are not covered by Realodix Readtime backwards compatibility guidelines. We may choose to rename function arguments when necessary in order to improve codebase. Therefore, using named arguments when calling methods should be done cautiously and with the understanding that the parameter names may change in the future.
Methods
get()
Retrieve the read time.
setTranslation(array $translations)
Manually set the translation text for the class to use. If no key is passed it will default to the English counterpart. A complete translation array will contain the following:
[ 'less_than' => 'less than a minute', 'one_min' => '1 min read', 'more_than' => 'min read', ];
toArray()
Get the contents and settings of the class as an array.
toJson()
Get the contents and settings of the class as a JSON string.
[ 'duration' => '6 min read', 'actual_duration' => 5.55, 'total_words' => 1325, 'total_words_cjk' => 0, 'total_images' => 3, 'word_time' => 5.660, 'word_time_cjk' => 0, 'image_time' => 0.55, ];
Contributing
Thank you for your interest in ReadTime. Please check out our contributing guide.
License
This package is licensed using the MIT License.