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.
<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.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.substr()/strip_tags()/regex. Standardizing is the value; the specific library is mostly incidental.Consider it when:
Look elsewhere (or build a 50-line helper) when:
mews/purifier, symfony/html-sanitizer, or HTMLPurifier, or you will ship XSS through "truncated" user content.DOMDocument or a real HTML parser; character-slicing HTML is the wrong tool.league/commonmark.strlen/substr will corrupt content in non-Latin locales, and retrofitting that is a visible, user-facing bug.Adoption guardrails if you proceed:
<img>, <table>, entities, emoji) — those tests are the real asset, not the library.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.
How can I help you explore Laravel packages today?