定制 nojimage/twitter-text-php 二次开发

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

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

nojimage/twitter-text-php

Composer 安装命令:

composer require nojimage/twitter-text-php

包简介

A library of PHP classes that provide auto-linking and extraction of usernames, lists, hashtags and URLs from tweets.

README 文档

README

A library of PHP classes that provide auto-linking and extraction of usernames, lists, hashtags and URLs from tweets. Originally created from twitter-text-rb and twitter-text-java projects by Matt Sanford and ported to PHP by Mike Cochrane, this library has been improved and made more complete by Nick Pope.

Build Status Codecov Latest Stable Version

Requirements

  • PHP 7.4 or higher
  • ext-mbstring
  • ext-intl

If the necessary extensions are not installed on the server, please install it additionally or use symfony/polyfill.

Install

You can install this library into your application using Composer.

composer require nojimage/twitter-text-php

Note for Older Server

This library use intl/libICU. Some older server and PHP 7.2+ combinations may have deprecated warnings due to older ICU versions. refs #32

If you are using RHEL/CentOS 6, installing PHP using the remi repository is the best choice. If you use remi, you can use the new ICU.

Features

Autolink

  • Add links to all matching Twitter usernames (no account verification).
  • Add links to all user lists (of the form @username/list-name).
  • Add links to all valid hashtags.
  • Add links to all URLs.
  • Support for international character sets.

Extractor

  • Extract mentioned Twitter usernames (from anywhere in the tweet).
  • Extract replied to Twitter usernames (from start of the tweet).
  • Extract all user lists (of the form @username/list-name).
  • Extract all valid hashtags.
  • Extract all URLs.
  • Support for international character sets.

Hit Highlighter

  • Highlight text specifed by a range by surrounding with a tag.
  • Support for highlighting when tweet has already been autolinked.
  • Support for international character sets.

Validation

  • Validate different twitter text elements.
  • Support for international character sets.

Parser

  • Parses a given tweet text with the weighted character count configuration.

Length validation

twitter-text 3.0 updates the config file with emojiParsingEnabled config option. When true, twitter-text-php will parse and discount emoji supported by the Unicode Emoji 11.0 (NOTE: Original twitter-text supported twemoji library). The length of these emoji will be the default weight (200 or two characters) even if they contain multiple code points combined by zero-width joiners. This means that emoji with skin tone and gender modifiers no longer count as more characters than those without such modifiers.

twitter-text 2.0 introduced configuration files that define how Tweets are parsed for length. This allows for backwards compatibility and flexibility going forward. Old-style traditional 140-character parsing is defined by the v1.json configuration file, whereas v2.json is updated for "weighted" Tweets where ranges of Unicode code points can have independent weights aside from the default weight. The sum of all code points, each weighted appropriately, should not exceed the max weighted length.

Some old methods from twitter-text-php 1.0 have been marked deprecated, such as the Twitter\Text\Validator::isValidTweetText(), Twitter\Text\Validator::getTweetLength() method. The new API is based on the following method, Twitter\Text\Parser::parseTweet()

use Twitter\Text\Parser;
$parser = new Parser();
$result = $parser->parseTweet($text);

This method takes a string as input and returns a results object that contains information about the string. Twitter\Text\ParseResults object includes:

  • weightedLength: the overall length of the tweet with code points weighted per the ranges defined in the configuration file.

  • permillage: indicates the proportion (per thousand) of the weighted length in comparison to the max weighted length. A value > 1000 indicates input text that is longer than the allowable maximum.

  • valid: indicates if input text length corresponds to a valid result.

  • displayRangeStart, displayRangeEnd: An array of two unicode code point indices identifying the inclusive start and exclusive end of the displayable content of the Tweet. For more information, see the description of display_text_range here: Tweet updates

  • validRangeStart, validRangeRnd: An array of two unicode code point indices identifying the inclusive start and exclusive end of the valid content of the Tweet. For more information on the extended Tweet payload see Tweet updates

Examples

For examples, please see tests/example.php which you can view in a browser or run from the command line.

Conformance

You'll need the test data which is in YAML format from the following repository:

https://github.com/twitter/twitter-text

twitter/twitter-text already included in composer.json, so you should just need to run:

curl -s https://getcomposer.org/installer | php
php composer.phar install

There are a couple of options for testing conformance:

  • Run phpunit in from the root folder of the project.

Thanks & Contributions

The bulk of this library is from the heroic efforts of:

nojimage/twitter-text-php 适用场景与选型建议

nojimage/twitter-text-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.96M 次下载、GitHub Stars 达 119, 最近一次更新时间为 2013 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「text」 「twitter」 「extract」 「autolink」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nojimage/twitter-text-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nojimage/twitter-text-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 nojimage/twitter-text-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.96M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 125
  • 点击次数: 24
  • 依赖项目数: 10
  • 推荐数: 0

GitHub 信息

  • Stars: 119
  • Watchers: 6
  • Forks: 76
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2013-02-09