realodix/readtime

Medium 的阅读时间估算。

v1.6.0 2024-03-28 07:06 UTC

This package is auto-updated.

Last update: 2024-09-07 19:08:23 UTC


README

PHPVersion Build Status GitHub license

📚 Medium 的阅读时间估算,基于 Medium 的阅读时间公式

像 Medium.com 这样的网站普及了为用户提供阅读内容所需时间的估算概念。考虑到这种便利性,ReadTime 为 PHP 开发者提供了同样的工具来处理可读内容。这是一个简单的功能,将为您的 PHP 应用程序增添不少魅力。

特性

  • 按降序进度计算图片的阅读时间(例如,第一张图片为 12 秒,第二张为 11 秒,直到图片计数到 3 秒)。
  • 单独计算中文、日文、韩文字符的阅读时间。
  • 移除不需要的 HTML 标签以提高计算阅读时间的效率。

参考

安装

您可以通过 composer 安装此包。

composer require realodix/readtime

快速入门

以下是最基本的用法示例

<?php

use Realodix\ReadTime\ReadTime;

$readTime = new ReadTime('foo bar');

echo $readTime->get();
// less than a minute

如果您想动态更改设置,也可以向构造函数传递几个参数

/**
 * @param string|array $content   The content to analyze
 * @param int          $wpm       Speed of reading the text in Words per Minute
 * @param int          $imageTime Speed of reading the image in seconds
 * @param int          $cpm       Speed of reading the Chinese / Korean / Japanese
 *                                characters in Characters per Minute
 */

$readTime = new ReadTime($content, $wpm = 265, $imageTime = 12, $cpm = 500);

echo $readTime->get();

ReadTime 类能够接受一个内容字符串或一个包含多个内容片段的数组(扁平或多维)。如果您尝试显示正文内容和侧边栏内容的总阅读时间,这可能很有用。

例如

$readTime = new ReadTime([$content, $moreContent, $evenMoreContent]);

echo $readTime->get();

命名参数 不受 Realodix Readtime 向后兼容性指南的覆盖。在必要时,我们可能会选择重命名函数参数以改进代码库。因此,在调用方法时使用命名参数应谨慎进行,并理解参数名称可能在将来发生变化。

方法

get()

检索阅读时间。

setTranslation(array $translations)

手动设置类使用的翻译文本。如果没有传递键,则默认为英文对应项。完整的翻译数组将包含以下内容

[
    'less_than' => 'less than a minute',
    'one_min'   => '1 min read',
    'more_than' => 'min read',
];
toArray()

获取类的内容和设置,作为数组。

toJson()

获取类的内容和设置,作为 JSON 字符串。

[
    'duration'        => '6 min read',
    'actual_duration' => 5.55,
    'total_words'     => 1325,
    'total_words_cjk' => 0,
    'total_images'    => 3,
    'word_time'       => 5.660,
    'word_time_cjk'   => 0,
    'image_time'      => 0.55,
];

贡献

感谢您对 ReadTime 的兴趣。请查看我们的 贡献指南

许可

此包使用 MIT 许可证 许可。