PHP Luminova: Normalize and Shorten Text
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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | The text to process. |
$stripTags | bool | Whether to remove HTML tags before normalization (default: false). |
$strictWhitespace | bool | Whether 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
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | The text to truncate. |
$length | int | Maximum length of the resulting string (default: 10). |
$encoding | string | Character encoding (default: UTF-8). |
$ellipsis | bool | Whether 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | Message to search. |
$needle | string | Marker 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$message | string | Message to search. |
$needle | string | Marker 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | Input text. |
$strict | bool | Whether 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | Input 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | Input 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | Input 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): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | Input 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 = ''
): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$text | string | Input text. |
$strict | bool | Whether to remove designated elements and their contents (default: false). |
$retain | string[]\|string\|null | Tags to retain, as a list of names or a string such as <b><em> (default: null). |
$replacement | string\|callable | Text 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.