Estimated reading time macro in Laravel

1 · Amit Merchant · May 5, 2021, 3:34 p.m.
When you want to extend some of the classes’ functionality in Laravel, Macros are your best bet. Your imagination is the only limit to what you can do with macros. For instance, I have recently come across a handly little macro shared by Marcel Pociot that gives you the estimated reading time for the given text(s). Here’s how the macro looks like. use Illuminate\Support\Str; Str::macro('readDuration', function(...$text) { $totalWords = str_word_count(implode(" ", $text)); $minutesToRead...