Get estimated read time of an article. Similar to medium.com's "x min read". Multilingual including right-to-left written languages. Supports JSON, Array and String output.

Overview

Read Time

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

  • $wordsPerMinute default value is 200
  • $rtl language direction right-to-left is false by default
  • $translation default is null class outputs the English language by default
  • $abbreviate Abbreviate the word 'minute/minutes' to 'min' is true by 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)
}
You might also like...
A Laravel package for multilingual models
A Laravel package for multilingual models

Introduction If you want to store translations of your models into the database, this package is for you. This is a Laravel package for translatable m

Trait for multilingual resource file support

⚡ Usage This library supports MultilingualResourceTrait which can be used in PluginBase. Multilingual support of resource files is possible using this

Laravel Multilingual Models

Laravel Multilingual Models Make Eloquent model attributes translatable without separate database tables for translation values. Simply access $countr

Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.
Laravel Livewire (TALL-stack) form generator with realtime validation, file uploads, array fields, blade form input components and more.

TALL-stack form generator Laravel Livewire, Tailwind forms with auto-generated views. Support Contributions Features This is not an admin panel genera

These are simple array and object collections that provide convinient methods to manipulate them.

Simple Collections These are simple array and object collections that provide convinient methods to manipulate collections; To install this package ty

An advanced Laravel integration for Bref, including Octane support.

Bref Laravel Bridge An advanced Laravel integration for Bref, including Octane support. This project is largely based on code from PHP Runtimes, Larav

Provide all attributes (including irregular patterns) to Laravel Blade class components.

blade-wants-attributes blade-wants-attributes offers you the ability to use Blade/HTML-defined attributes within the constructors of Laravel Blade cla

Output complex, flexible, AJAX/RESTful data structures.

Fractal Fractal provides a presentation and transformation layer for complex data output, the like found in RESTful APIs, and works really well with J

This project uses dflydev/dot-access-data to provide simple output filtering for cli applications.

FilterViaDotAccessData This project uses dflydev/dot-access-data to provide simple output filtering for applications built with annotated-command / Ro

Comments
  • I edited the function name and I switched to camelCase and I modified…

    I edited the function name and I switched to camelCase and I modified…

    I edited the function name and I switched to camelCase and I modified the example in Persia, as we don't use (دقایق) for more than 1 minutes. It was surprising for me too that I noticed that English is the most fastest language in the world and maybe it's the main reason that we use it as an international language. I wish I could realize the speed of my language(Persian).

    I saw your code in PHPClasses and I voted for it yesterday.

    opened by ehsan957 0
Releases(v1.1)
Owner
Waqar Ahmed
Full Stack Developer Javascript, PHP, Laravel, React, SQL
Waqar Ahmed
Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string template and recompiles it if needed.

Laravel-fly-view Is an Extension of Laravel View Class which compiles String Template on the fly. It automatically detects changes on your string temp

John Turingan 16 Jul 17, 2022
Live Helper Chat - live support for your website. Featuring web and mobile apps, Voice & Video & ScreenShare. Supports Telegram, Twilio (whatsapp), Facebook messenger including building a bot.

Live helper chat It's an open-source powered application, which brings simplicity and usability in one place. With live helper chat you can bring live

Live Helper Chat 1.7k Dec 29, 2022
An abstraction layer to get data from array or a file with dot-notation

Alex Unruh - Config This library is based on the Laravel config concept. It values performance and was built on top of the library Dflydev Dot Access

Alexandre Odair 3 May 20, 2022
A Laravel 8 and Livewire 2 demo showing how to search and filter by tags, showing article and video counts for each tag (Polymorphic relationship)

Advanced search and filter with Laravel and Livewire A demo app using Laravel 8 and Livewire 2 showing how to implement a list of articles and tags, v

Sérgio Jardim 19 Aug 29, 2022
Validate your input data in a simple way, an easy way and right way. no framework required. For simple or large. project.

wepesi_validation this module will help to do your own input validation from http request POST or GET. INTEGRATION The integration is the simple thing

Boss 4 Dec 17, 2022
Websockets for Laravel. Done right.

Bring the power of WebSockets to your Laravel application. Drop-in Pusher replacement, SSL support, Laravel Echo support and a debug dashboard are just some of its features.

Beyond Code 4.6k Jan 6, 2023
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
Searches for multilingual phrases in Laravel project and automatically generates language files for you.

Laravel Lang Generator Searches for multilingual phrases in a Laravel project and automatically generates language files for you. You can search for n

Gleb 5 Oct 19, 2022
Links statistics & link tracking for laravel 5, It tracks down browsers, operating systems, languages and more

Links Links statistics for laravel 5 Table Of Contents Installation Configuration Usage Installation To install charts use composer Download composer

Erik C. Forés 52 Jul 31, 2021
Laravel 5 Package to Detect Users Browsers, Devices, Languages and Operating Systems

laravel-identify Laravel 5 Package to identify a User's Browser, Operating System, Language and Device Installation PHP 7.1+ or HHVM 3.3+, and Compose

Prosper Otemuyiwa 183 Nov 30, 2022