Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Truncate Html Laravel Package

urodoz/truncate-html

urodoz/truncate-html is a lightweight PHP library for safely truncating HTML strings to a chosen length without breaking tags. It preserves valid markup, supports custom ellipsis and encoding, and is ideal for generating clean excerpts from rich text in Laravel or any PHP project.

View on GitHub
Deep Wiki
Context7

Product Decisions This Supports

  • Excerpt/teaser surfaces: This is the core job — rendering a shortened preview of rich-text content without producing broken markup (unclosed <b>, dangling <a href>, cut-off entities). It directly supports decisions about how listings (blog index, category pages, search results, "related content", activity feeds) display user- or editor-authored HTML.
  • Notification and email previews: Push/email/in-app notifications need a short text or HTML snippet of a longer document. Broken tags in an email preview are a visible quality bug; a tag-aware truncator removes a class of support tickets.
  • SEO/metadata generation: Deriving meta description, Open Graph, and Twitter card descriptions from article bodies. This is a "generate or author" product decision — automated derivation is cheaper but only viable if the truncation output is clean and word-boundary-aware.
  • Build vs. buy (really "adopt vs. write"): The honest framing here is not buy-vs-build against a vendor — this is a small utility. The PM decision is whether to standardize excerpt behavior across N surfaces via one dependency, versus letting each team hand-roll substr()/strip_tags()/regex. Standardizing is the value; the specific library is mostly incidental.
  • Roadmap signal: Multiple teams asking for excerpt logic is a signal that the product needs a defined "content presentation" layer (excerpt length rules, fallback to plain text, ellipsis policy, per-locale rules). The package is a stopgap that surfaces the policy questions.
  • Concrete use cases worth scoping: CMS/blog teasers, e-commerce product description previews, knowledge base search snippets, forum/discussion thread previews, admin moderation queues (showing truncated flagged content), RSS/feed generation.

When to Consider This Package

Consider it when:

  • Your source content is genuinely HTML, not Markdown or plain text, and you need to shorten it while keeping the markup well-formed.
  • The same truncation behavior is needed in multiple call sites (models/accessors, API resources, Blade partials) and you want one consistent rule.
  • You want a tiny, MIT-licensed, auditable dependency rather than maintaining bespoke regex in your codebase. Read the source before adopting — at this size it is a short read.
  • Your requirements are simple: character/word limit, preserve or strip tags, append ellipsis.

Look elsewhere (or build a 50-line helper) when:

  • You also need sanitization. Truncation is not a security control — pair with mews/purifier, symfony/html-sanitizer, or HTMLPurifier, or you will ship XSS through "truncated" user content.
  • You need DOM manipulation (rewriting links, stripping images, extracting the first paragraph). Use DOMDocument or a real HTML parser; character-slicing HTML is the wrong tool.
  • You need Markdown handling — use league/commonmark.
  • You need locale-aware / grapheme-cluster truncation (CJK, emoji, Arabic). Verify the implementation handles multibyte correctly before committing; a naive strlen/substr will corrupt content in non-Latin locales, and retrofitting that is a visible, user-facing bug.
  • Maintenance maturity matters. 21 stars, 0 dependents, and a low opportunity score mean effectively no community and a bus factor of roughly one. Do not adopt it as an unexamined transitive dependency or in a critical path without a plan.

Adoption guardrails if you proceed:

  • Pin the version and commit/vendor it if the repo looks stale.
  • Wrap it behind your own interface or a Blade directive / model accessor so it is swappable in one place.
  • Write characterization tests around your actual content corpus (nested tags, <img>, <table>, entities, emoji) — those tests are the real asset, not the library.
  • Assume you may fork it; keep the license file.

How to Pitch It (Stakeholders)

For executives: This is a small, low-cost, permissively licensed utility that makes content previews — listing pages, search snippets, email/push previews, SEO descriptions — render cleanly instead of showing broken formatting. The business value is consistency and reduced presentation bugs across surfaces, not new capability. Cost and risk are minimal, but so is the ecosystem around it: treat it as a component we control, not a vendor we depend on. Recommendation: approve as a scoped, wrapped implementation detail, not as a platform commitment.

For engineering: MIT-licensed HTML-aware truncator for excerpt generation. It solves tag-safe, word-boundary truncation, which we currently reimplement ad hoc per surface. Caveats: very small community (21 stars, 0 dependents), so we should pin the version, read the source, wrap it behind an adapter (interface or Blade directive), and cover it with tests built from our real content. It is not a sanitizer and not a DOM tool — pair with a sanitizer for user-generated content and use DOMDocument if we need structural rewriting. If the repo proves unmaintained, the adapter makes forking or replacing it a one-file change.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus