StringUtil
Raxos\Foundation\Util\StringUtil is a final class of static helper methods for common string manipulation tasks.
See the Util classes concept page for an overview of all utility classes.
Signature
namespace Raxos\Foundation\Util;
final class StringUtilMethods
public static function slugify(string $str): stringTransliterates and normalises a string into a URL friendly slug.
public static function toPascalCase(string $str): stringConverts a string to PascalCase.
public static function toSnakeCase(string $str): stringConverts a string to snake_case.
public static function truncateText(string $text, int $wordCount = 20, string $ending = '...'): stringStrips tags and headings and truncates text to a maximum word count.
public static function formatBytes(int $value, int $decimals = 2, bool $siMode = true, bool $bits = false): stringFormats a byte or bit count into a human readable string with SI or IEC suffixes.
public static function random(int $length = 9, bool $dashes = false, string $sets = 'luds'): stringGenerates a random string from lowercase, uppercase, digit and symbol character sets, optionally dash separated.
public static function shortClassName(string $className): stringReturns the class name without its namespace.
public static function commaCommaAnd(array $strings): stringJoins a list of strings with commas and a trailing and.
public static function splitSentences(string $str): arraySplits a string into an array of sentences.
public static function isSerialized(string $data): boolReturns true if the string is a serialised PHP value.
public static function multiByteSubstringReplace(string $str, string $replacement, int $start, ?int $length = null): stringA multibyte aware variant of substr_replace.
Example
<?php
declare(strict_types=1);
use Raxos\Foundation\Util\StringUtil;
$slug = StringUtil::slugify('Café del Mar'); // 'cafe-del-mar'
$size = StringUtil::formatBytes(1_500_000); // '1.5 MB'
$name = StringUtil::shortClassName(StringUtil::class); // 'StringUtil'