Skip to content

Instantly share code, notes, and snippets.

@bran921007
Forked from owenvoke/readDuration.php
Created May 5, 2021 19:18
Show Gist options
  • Save bran921007/6fe34f56c383c215bb9dba2e383a9679 to your computer and use it in GitHub Desktop.
Save bran921007/6fe34f56c383c215bb9dba2e383a9679 to your computer and use it in GitHub Desktop.
A string macro for the duration to read text.
<?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