PHP code example of leshkens / laravel-read-time

1. Go to this page and download the library: Download leshkens/laravel-read-time library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leshkens / laravel-read-time example snippets

 bash
php artisan vendor:publish --provider="Leshkens\LaravelReadTime\Providers\ReadTimeServiceProvider"
 php 
'counter' => Leshkens\LaravelReadTime\Counter::class,
 php 
public function count(string $content): int
{
    return count(preg_split('/\s+/', $content, -1, PREG_SPLIT_NO_EMPTY));
}
 php 
'locales' => [
    'en' => Leshkens\LaravelReadTime\Locales\En::class
]
 php 
namespace App\Support\ReadTimeLocales;

use Leshkens\LaravelReadTime\Contracts\LocaleInterface;
use function morphos\Russian\pluralize;

class Ru implements LocaleInterface
{
    protected $unitMap = [
        'second' => 'секунда',
        'minute' => 'минута',
        'hour'   => 'час'
    ];

    public function result(int $number, string $unit): string
    {
        return pluralize($number, $this->unitMap[$unit]);
    }
}
 php 
readtime($content, $locale, $options);
 php 
// array
[
    'ru' => 'Какой-то прекрасный текст',
    'en' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
]

// or json
{
    "ru": "Какой-то прекрасный текст",
    "en": "Lorem ipsum dolor sit amet, consectetur adipiscing elit"
}
 php 
protected function readTime(): array
{
    return [
        'source'    => 'content',
        'localable' => true
    ];
}
 php
[
  'ru' => '1 секунда'
  'en' => '3 seconds on read'
]