Skip to content

Instantly share code, notes, and snippets.

@dhanar98
Created January 15, 2024 16:33
Show Gist options
  • Save dhanar98/d3e65856261a3b46a6c9a1cf9ac1758b to your computer and use it in GitHub Desktop.
Save dhanar98/d3e65856261a3b46a6c9a1cf9ac1758b to your computer and use it in GitHub Desktop.
Reading Time in Blog Post Using laravel Str:wordCount() Function In Helper
<?php
# Filename : app/helpers.php
use Illuminate\Support\Str;
if (!function_exists('calculateReadingTime')) {
function calculateReadingTime($content)
{
$word_count = Str::wordCount($content);
$reading_time = ceil($word_count / 200);
if ($reading_time == 1) {
$timer = " minute";
} else {
$timer = " minutes";
}
$total_reading_time = $reading_time . $timer;
return $total_reading_time;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment