Last active
July 22, 2019 16:45
-
-
Save AlekVolsk/b1dca23f9c443eae95b0916c242ae9c7 to your computer and use it in GitHub Desktop.
Getting time to read the article with the images in it
This file contains hidden or 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
<?php | |
function getReadingTime($content) | |
{ | |
$wordsPerMinuten = 200; // ru: слов в минуту | |
$secondsPerImage = 5; // ru: секунд на изображение | |
preg_match_all("~<img~i", $content, $resImgs); | |
$imgsTime = count($resImgs[0]) * $secondsPerImage; | |
$words = count(explode(' ', strip_tags(str_replace(["\n", '><'], ' ', $content)))); | |
$allSeconds = ($words / $wordsPerMinuten) * 60 + $imgsTime; | |
$minutes = floor($allSeconds / 60); | |
$seconds = round(($allSeconds % 60) / 10) * 10; | |
return trim(($minutes ? $minutes . ' min. ' : '') . ($seconds ? $seconds . ' sec.' : '')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment