Skip to content

Instantly share code, notes, and snippets.

@tfevens
Last active May 5, 2021 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tfevens/55eaf238c9225d7533d8c4987a7c35f2 to your computer and use it in GitHub Desktop.
Save tfevens/55eaf238c9225d7533d8c4987a7c35f2 to your computer and use it in GitHub Desktop.
This snippet returns an integer, for the estimated reading time for a piece of text. Think of the avg time on most blog posts.
Str::macro('readingTime', function (...$text) {
$totalWords = str_word_count(implode(" ", $text));
$minutesToRead = round($totalWords / 200); // Avg WPM of adult
return (int)max(1, $minutesToRead);
});
@tfevens
Copy link
Author

tfevens commented May 5, 2021

Credit to @marcelpociot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment