Created
July 24, 2012 14:55
-
-
Save mynameispj/3170442 to your computer and use it in GitHub Desktop.
Estimated reading time in PHP, by Brian Cray
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
In Drupal 7: | |
<?php | |
$postContent = render($content); | |
$word = str_word_count(strip_tags($postContent)); | |
$m = floor($word / 200); | |
$s = floor($word % 200 / (200 / 60)); | |
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's'); | |
?> | |
<p>Estimated reading time: <?php echo $est; ?></p> |
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
Brian Cray's original, for Wordpress: | |
<?php | |
$mycontent = $post->post_content; // wordpress users only | |
$word = str_word_count(strip_tags($mycontent)); | |
$m = floor($word / 200); | |
$s = floor($word % 200 / (200 / 60)); | |
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's'); | |
?> | |
<p>Estimated reading time: <?php echo $est; ?></p> |
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
Total props to Brian Cray: http://briancray.com/posts/estimated-reading-time-web-design/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@stemword According to web, average reading speed is between 230-280 words per minute, so he using 200 here is keeping some limit to provide pessimistic reading time. Most of people should read it faster.