Last active
November 13, 2022 14:17
-
-
Save MohcinBN/799d99ff9f5001aeb78dc0adf3c89b6b to your computer and use it in GitHub Desktop.
Laravel 9 reading time for yuour posts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// add this function to your post model; | |
// reading time | |
//The average reader reads about 200 words per minute, so I decided to have less to make estimates more accurate | |
public function postReadingTimeEstimation($averageReadsPerMunite = 180) | |
{ | |
$textOfTheBody = $this->body; | |
$totalWords = str_word_count(strip_tags($textOfTheBody)); | |
$minutes = floor($totalWords / $averageReadsPerMunite); | |
$seconds = floor($totalWords % $averageReadsPerMunite / ($averageReadsPerMunite / 60)); | |
if ($minutes < 60) { | |
$minutes = 1 . ' minutes'; | |
} | |
return $minutes; | |
} | |
// call the function into your view | |
{{ $post->postReadingTimeEstimation() }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment