Luminova Framework

PHP Luminova: Normalize and Shorten Text

Last updated: 2026-11-28 20:57:12

Normalize whitespace and line breaks, extract text around a marker, and shorten multibyte strings.

Use Text to prepare labels, excerpts, and imported text for display. These operations change the string itself. Apply the output escaping appropriate to the destination when rendering untrusted input.


Usage

use Luminova\Utility\Text;

$label = Text::normalize('  Amina   Yusuf  ');
echo $label; // Amina Yusuf

echo Text::before('Summary: Draft', ':'); // Summary
echo Text::after('Summary: Draft', ':'); // Draft
echo Text::truncate('Amina Yusuf', 8, ellipsis: true); // Amina...

Class Definition

  • Full namespace: Luminova\Utility\Text
  • This class is final.

Methods

normalize

Normalize whitespace, line breaks, and Unicode text. Pass stripTags: true to run DOM-based tag removal. The result can retain the XML declaration added by the DOM parser.

public static function normalize(
    string $text,
    bool $stripTags = false,
    bool $strictWhitespace = true
): string

Parameters:

ParameterTypeDescription
$textstringThe text to process.
$stripTagsboolWhether to remove HTML tags before normalization (default: false).
$strictWhitespaceboolWhether to normalize only spaces and non-breaking spaces (default: true).

Return:

string - Return normalized text.


truncate

Truncate a string in specified length and adds an ellipsis at the end if the text is longer than the specified length.

public static function truncate(
    string $text,
    int $length = 10,
    string $encoding = 'UTF-8',
    bool $ellipsis = false
): string

Parameters:

ParameterTypeDescription
$textstringThe text to truncate.
$lengthintMaximum length of the resulting string (default: 10).
$encodingstringCharacter encoding (default: UTF-8).
$ellipsisboolWhether to append an ellipsis when truncating (default: false).

Return:

string - The truncated string.


before

Return the trimmed text before the first case-insensitive match. If the marker is empty or absent, return the trimmed message.

public static function before(string $message, string $needle): string

Parameters:

ParameterTypeDescription
$messagestringMessage to search.
$needlestringMarker to locate.

Return:

string - Trimmed result.


after

Return the trimmed text after the first case-insensitive match. If the marker is empty or absent, return the trimmed message.

public static function after(string $message, string $needle): string

Parameters:

ParameterTypeDescription
$messagestringMessage to search.
$needlestringMarker to locate.

Return:

string - Trimmed result.


normalizeWhitespace

Collapse runs of two or more whitespace characters while preserving carriage returns and line feeds.

public static function normalizeWhitespace(string $text, bool $strict = false): string

Parameters:

ParameterTypeDescription
$textstringInput text.
$strictboolWhether to collapse only runs of spaces and non-breaking spaces (default: false).

Return:

string - Text with whitespace normalized.


normalizeNewlines

Convert HTML breaks and line endings to LF, remove trailing horizontal whitespace from each line, and limit runs of newlines to two.

public static function normalizeNewlines(string $text): string

Parameters:

ParameterTypeDescription
$textstringInput text.

Return:

string - Text with normalized line endings.


normalizeHtmlNewlines

Replace <br>, <br/>, and their case or whitespace variants with LF.

public static function normalizeHtmlNewlines(string $text): string

Parameters:

ParameterTypeDescription
$textstringInput text.

Return:

string - Text with HTML breaks converted.


normalizeUnicode

Apply Unicode NFC normalization when the PHP normalizer is available.

public static function normalizeUnicode(string $text): string

Parameters:

ParameterTypeDescription
$textstringInput text.

Return:

string - Normalized string or the input if normalization is unavailable or unsuccessful.


normalizeInvisible

Remove control and Unicode format characters while preserving carriage returns, line feeds, and horizontal tabs.

public static function normalizeInvisible(string $text): string

Parameters:

ParameterTypeDescription
$textstringInput text.

Return:

string - Text with the selected characters removed.


stripTags

Parse markup using PHP DOM. Retained elements stay in the result. Other elements are unwrapped unless strict mode removes them with their contents. Use this for text processing. For HTML security, apply context-appropriate escaping or a sanitizer. The DOM output can include its UTF-8 XML declaration.

public static function stripTags(
    string $text,
    bool $strict = false,
    array|string|null $retain = null,
    string|callable $replacement = ''
): string

Parameters:

ParameterTypeDescription
$textstringInput text.
$strictboolWhether to remove designated elements and their contents (default: false).
$retainstring[]\|string\|nullTags to retain, as a list of names or a string such as <b><em> (default: null).
$replacementstring\|callableText to place at each removed element or a callable that receives the tag name (default: '').

Return:

string - Processed text, or the original input if parsing fails.