定制 midesweb/reading-time 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

midesweb/reading-time

最新稳定版本:v0.3.2

Composer 安装命令:

composer require midesweb/reading-time

包简介

Reading time PHP class calculator

README 文档

README

EstimatedReadingTime is a lightweight PHP utility that calculates the estimated reading time (in minutes) for a given block of HTML or plain text content.

It uses a simple heuristic: an average reading speed of 120 words per minute. It strips HTML tags from the content before counting the words and rounds the result to the nearest full minute (rounding up if the leftover seconds exceed 30 seconds).

✅ Features

  • Automatically strips HTML tags before processing
  • Calculates reading time based on word count
  • Easy to update the content dynamically
  • Supports human-readable output in English (EN) and Spanish (ES)

🔖 Installation

composer require midesweb/reading-time

🧱 Basic Example

use Midesweb\ReadingTime\EstimatedReadingTime;

$content = "<p>This is a sample article with some <strong>HTML</strong> content.</p>";
$estimator = new EstimatedReadingTime($content);

$minutes = $estimator->getEstimatedReadingMinutes();

echo "Estimated reading time: {$minutes} minute(s)";

🔁 Updating the content

$estimator->updateContent("New content goes here...");
$minutes = $estimator->getEstimatedReadingMinutes();

🌍 Localized Read Time Output

You can get a localized string describing the estimated read time using getReadTime().
The default language is English (EN), but you can switch to Spanish (ES) using setLanguage():

$estimator = new EstimatedReadingTime($content);

// English (default)
echo $estimator->getReadTime(); 
// Output: "1 minute", "3 minutes", "1 hour and 5 minutes", etc.

// Spanish
echo $estimator->setLanguage('es')->getReadTime();
// Output: "1 minuto", "3 minutos", "1 hora y 5 minutos", etc.

🔒 Only EN and ES are supported. Invalid languages will throw an InvalidArgumentException.

🧪 How it works

  1. Strips HTML tags from the input.
  2. Counts words using str_word_count().
  3. Divides by 120 (average words per minute).
  4. Rounds up the result if more than 30 seconds remain.
  5. Optionally returns a localized human-readable string.

💡 Requirements

  • PHP 7.4 or higher

统计信息

  • 总下载量: 2
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-06-11