PHP Joomla! 6 Joomla! 5 Joomla! 3 (Legacy) Template Override Intermediate

Truncate and Strip HTML from Article Text Safely

Strips HTML tags, normalizes whitespace and truncates UTF-8 text on word boundaries using core HTMLHelper string.truncate.

Updated on 24 November 2026 54 copies 219 views
templates/seu_template/html/com_content/category/blog_item.php
<?php

declare(strict_types=1);

use Joomla\CMS\HTML\HTMLHelper;

$maxLimit = 200;
$clean    = preg_replace('/<(?:br|p)[^>]*>/i', ' ', (string) $text);
$clean    = trim((string) preg_replace('/\s+/u', ' ', strip_tags((string) $clean)));

echo HTMLHelper::_('string.truncate', $clean, $maxLimit, true, false);
Uses core HTMLHelper string.truncate with full UTF-8 multibyte safety.
templates/seu_template/html/com_content/category/blog_item.php
<?php
// Limite de caracteres
$maxLimit = 200;
$text = preg_replace("/\r\n|\r|\n/", ' ', $text);
$text = preg_replace('/<BR[^>]*>/i', ' ', $text);
$text = preg_replace('/<P[^>]*>/i', ' ', $text);
$text = strip_tags($text);
$text = substr($text, 0, $maxLimit);
$text = preg_replace('/[.,!?:;]? [^ ]*$/', ' ', $text);
$text = trim($text) . '...';
$text = str_replace("\n", ' ', $text);

echo $text;
Manual regex and strip_tags implementation from Joomla 3.

Technical explanation and best practices

When building clean article excerpts for blog cards or search lists, strip HTML safely and truncate on UTF-8 word boundaries so accented characters are never corrupted.

Joomla 5 and 6 provides HTMLHelper::_('string.truncate', $text, $limit, true, false) natively with full multibyte support.

Direct Engineering

Have a Joomla project or technical challenge in mind?

From architectural diagnosis to production deployment, directly with the engineer.