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.
Architecture fit
mews/purify) or upstream of a strict output escaper, never as the sole trust boundary.Integration feasibility
composer.json constraints. At 21 stars and effectively no release cadence, expect a php require in the 5.4–7.x range. Verify on the target runtime (PHP 8.1/8.2/8.3) before committing; a hard constraint failure means a fork or a rewrite, and a rewrite of this algorithm is roughly a day of work.DOMDocument-based and therefore deterministic and easy to golden-test, which lowers integration risk considerably.Technical risk
&-style entities mid-sequence, truncating inside <script>/<style>, closing void elements incorrectly, and mishandling nested or malformed input from user-generated content.Key questions
<script>/<style> content?Stack fit
Migration path
HtmlTruncator interface with truncate(string $html, int $length, array $options): string. Implement it with this package. All call sites depend on the interface.DomDocument-based implementation (load with LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD, walk text nodes to the budget, serialize) behind the same interface. This costs little and converts an abandonment risk into a config flag.app/Support with the MIT notice, add tests, and drop the Composer dependency entirely.Compatibility
composer.json; do not allow floating ranges on a package with no release discipline.mb_internal_encoding('UTF-8') and with non-ASCII content, emoji, and CJK text.Truncate/Str helper in the app.Sequencing
Maintenance
Support
Scaling
excerpt column or a cache key keyed on content hash + length) rather than truncating at read time.Failure modes
or a halved emoji produces visible garbage. Mitigation: fixture tests; prefer word-boundary truncation.<script> or preserving event-handler attributes. Mitigation: strip these before truncation, sanitize after.Ramp-up
How can I help you explore Laravel packages today?