Skip to content

Instantly share code, notes, and snippets.

@AlekVolsk
Last active July 22, 2019 16:45
Show Gist options
  • Save AlekVolsk/b1dca23f9c443eae95b0916c242ae9c7 to your computer and use it in GitHub Desktop.
Save AlekVolsk/b1dca23f9c443eae95b0916c242ae9c7 to your computer and use it in GitHub Desktop.
Getting time to read the article with the images in it
<?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