-
-
Save bran921007/6fe34f56c383c215bb9dba2e383a9679 to your computer and use it in GitHub Desktop.
A string macro for the duration to read text.
This file contains hidden or 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