Skip to content

Instantly share code, notes, and snippets.

@owenvoke
Created May 5, 2021 10:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save owenvoke/80590ec6f4c066efbf29bffefc6ddb70 to your computer and use it in GitHub Desktop.
Save owenvoke/80590ec6f4c066efbf29bffefc6ddb70 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