Learn how to easily determine the estimated reading of any text (HTML, Markdown) with plain PHP.

How to determine the estimated reading time of a text with PHP

According to multiple studies and statistics, the measurement of speed and comprehension depends upon the text contents and upon a set of questions. The following table that doesn't belong to a specific test gives us a general idea of the reading efficiencies:

Screen Paper Comprehension Reader profile
100 wpm 110 wpm 50% Insufficient
200 wpm 240 wpm 60% Average reader
300 wpm 400 wpm 80% Good reader
700 wpm 1000 wpm 85% Excellent, accomplished reader

According to this table, the average reader reads about 200 words per minute. Using this number, we can easily determine the time that it would take for a person to read an article if we have the number of words that the text contains.

In this article, I will explain to you how to easily determine the estimated reading time of any text (in HTML, markdown or plain text) using a simple PHP function. Of course, you need to know that it's quite difficult to generate a 100% accurate estimate reading time as it will always vary, however, mathematically you can provide this statistic to your users and your website will look nicer.

Implementation

The implementation logic goes like this. We will need the content that we'll use to estimate the reading time, this text will be ideally in plain text, so before using the logic to determine how long does it take to read it, we need to remove any content that doesn't count as plain text. For example, if you provide your content as HTML there are multiple things in the content that would generate undesired output, just like the following content, because if we use the str_word_count function of PHP to count the words of the following string, it would count 8 words (if you analyze the content they're really 8 written words) but there's only a single word in the text namely "Welcome":

<h1> <span class="highlight">Wel</span>come </h1>

So, to avoid this inaccuracy, we need to stripe the HTML tags using the strip_tags function of PHP which would generate the plain text from our HTML:

 Welcome 

Once you have the number of words, you can simply calculate the minutes it would take to read the content simply by dividing the number of words between the rate of words per minute that a person can read (200 wpm is a good average, but you can change it). This will provide you with a good estimate of how many minutes it would take a regular user to read your content. If you want to extract the seconds as well, you only need to divide the remainder of the division (between the total words and the words per minute ) between the words per minute rate divided by 60 seconds.

Having said that, you can implement the function like this, which receives as first argument the unprocessed text (HTML, Markdown etc) and as second argument the words per minute rate that you want to use to calculate the reading time. By default it will use 200 words per minute, but you can provide your own rate if you need to. The function will return an associative array with 2 keys (minutes and seconds) that contain respectively the amount of time needed to read the text:

/**
 * Function to calculate the estimated reading time of the given text.
 * 
 * @param string $text The text to calculate the reading time for.
 * @param string $wpm The rate of words per minute to use.
 * @return Array
 */
function estimateReadingTime($text, $wpm = 200) {
    $totalWords = str_word_count(strip_tags($text));
    $minutes = floor($totalWords / $wpm);
    $seconds = floor($totalWords % $wpm / ($wpm / 60));
    
    return array(
        'minutes' => $minutes,
        'seconds' => $seconds
    );
}

This function can be easily used like this:

// ["minutes" => 0, "seconds" => 2]
estimateReadingTime("<h1>Title</h1><p>This is the content of the article</p>");

Happy coding ❤️!


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors