marcgoertz/shorten 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

marcgoertz/shorten

Composer 安装命令:

composer require marcgoertz/shorten

包简介

Safely truncate HTML markup while preserving tags, handling entities, and supporting Unicode/emoji with optional word-safe truncation

README 文档

README

Safely truncate HTML markup while preserving tags, handling entities, and supporting Unicode/emoji with optional word-safe truncation.

Test Coverage Status Packagist PHP Version Support Packagist Downloads Packagist Stars MIT License

Installation

I recommend using Composer for installing and using Shorten:

composer require marcgoertz/shorten

Of course you can also just require it in your scripts directly.

Usage

<?php

use Marcgoertz\Shorten\Shorten;

$shorten = new Shorten();
print $shorten->truncateMarkup('<a href="https://example.com/">Go to example site</a>', 10);
?>

Output:

<a href="https://example.com/">Go to exam</a>

Functions

truncateMarkup()

truncateMarkup(
    string $markup,
    int $length = 400,
    string $appendix = '',
    bool $appendixInside = false,
    bool $wordsafe = false,
    string $delimiter = ' '
): string

Parameters

  • string $markup: Text containing markup
  • int $length: Maximum length of truncated text (default: 400)
  • string $appendix: Text added after truncated text (default: '…')
  • bool $appendixInside: Add appendix to last content in tags, increases $length by 1 (default: false)
  • bool $wordsafe: Wordsafe truncation, cuts at word boundaries (default: false)
  • string $delimiter: Delimiter for wordsafe truncation (default: ' ')

Examples

<?php
use Marcgoertz\Shorten\Shorten;

$shorten = new Shorten();

// Basic truncation
$result = $shorten->truncateMarkup('<b>Hello world test</b>', 10);
// Output: <b>Hello worl</b>…

// Appendix inside tags
$result = $shorten->truncateMarkup('<b>Hello world test</b>', 10, '...', true);
// Output: <b>Hello worl...</b>

// Wordsafe truncation (cuts at word boundaries)
$result = $shorten->truncateMarkup('<b>Hello world test</b>', 10, '...', false, true);
// Output: <b>Hello</b>...

// Custom delimiter for wordsafe truncation
$result = $shorten->truncateMarkup('<b>Hello-world-test</b>', 10, '...', false, true, '-');
// Output: <b>Hello</b>...

// Preserves HTML structure with nested tags
$result = $shorten->truncateMarkup('<div><b><i>Hello world</i></b></div>', 8);
// Output: <div><b><i>Hello wo</i></b></div>…

// Handles HTML entities correctly
$result = $shorten->truncateMarkup('<b>Caf&eacute; &amp; Restaurant</b>', 8);
// Output: <b>Café &amp; Re</b>…
?>

Features

  • ✅ Preserves HTML tag structure and proper nesting
  • ✅ Handles HTML entities correctly
  • ✅ Supports self-closing tags (both XML and HTML5 style)
  • ✅ UTF-8 and multibyte character support (including emojis)
  • ✅ Wordsafe truncation to avoid cutting words in the middle
  • ✅ Configurable appendix text and placement

Related

License

MIT © Marc Görtz

marcgoertz/shorten 适用场景与选型建议

marcgoertz/shorten 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.47k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2015 年 05 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 marcgoertz/shorten 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 9.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 12
  • 点击次数: 28
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-02