-
-
Save akoepcke/ce58479300f3f74bfc696b17d61364f9 to your computer and use it in GitHub Desktop.
A string macro for the duration to read text.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// From Marcel Pociot's tweet | |
// See: https://twitter.com/marcelpociot/status/1389881758267625473 | |
Str::macro('readDuration', function (... $text) { | |
$totalWords = str_word_count(implode(' ', $text)); | |
$minutesToRead = round($totalWords / 200); | |
return (int) max(1, $minutesToRead); | |
}); | |
echo Str::readDuration($post->text) . ' min read'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment