Helpers

PHP Reusable Helper Class

A namespaced utility class for URL-safe slugs and text excerpts.

Overview

A namespaced utility class for URL-safe slugs and text excerpts.

Code example

<?php
declare(strict_types=1);
namespace App\Support;
final class Text {
    public static function slug(string $v): string {
        $v=mb_strtolower(trim($v),'UTF-8'); $v=preg_replace('/[^\pL\pN]+/u','-',$v)??''; return trim($v,'-');
    }
    public static function excerpt(string $v,int $max=160): string {
        $v=trim((string)preg_replace('/\s+/u',' ',$v)); return mb_strlen($v)<=$max?$v:mb_substr($v,0,max(0,$max-1)).'…';
    }
}

How to use it

Place the snippet in a namespaced helper or service file, adapt its inputs to your application and add tests for expected and edge-case behavior.

Notes

The example is intentionally small. Validate untrusted input, handle exceptions and document any PHP extension or database requirements.