Skip to content

Instantly share code, notes, and snippets.

@Velman04
Last active May 3, 2023 13:00
Show Gist options
  • Save Velman04/7b33c34e572a28d8749d41ab22a568bb to your computer and use it in GitHub Desktop.
Save Velman04/7b33c34e572a28d8749d41ab22a568bb to your computer and use it in GitHub Desktop.
Text reading time | Время чтения текста
public function textReadingTime($text, $wpm = 120, $pause = 0.5): int
{
preg_match_all('/\w+/u', $text, $words);
$wordCount = count($words[0]);
$totalPauses = substr_count($text, '.')
+ substr_count($text, '!')
+ substr_count($text, '?');
$totalPauseTime = $totalPauses * $pause;
$readingTime = ($wordCount / $wpm) * 60 + $totalPauseTime;
return round($readingTime);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment