PHP Luminova: PHP String Object Helper Class
An object-oriented, string object utility class for PHP. It simplifies string manipulation with built-in methods for casing, trimming, masking, pattern matching, slug generation, and prototype.
The Str class offers a fluent, object-oriented interface for string manipulation in PHP.It combines readability, power, and flexibility, making it easy to perform common string operations without breaking flow or readability.
You can transform, compare, extract, mask, or hash strings, all through chainable methods.In addition, Str implements the PrototypeableInterface, allowing you to dynamically add new methods or properties at runtime, similar to JavaScript’s prototype model.
Key Features
- Fluent API – Most methods are chainable for cleaner code.
- Safe and predictable – Avoids mutating the original string unless explicitly set.
- Prototypeable – Extend the class at runtime with your own methods or properties.
- Utility-rich – Includes case conversions, trimming, masking, formatting, and more.
- Object-friendly – Implements PHP’s
Stringableinterface.
Usage
The Str class provides an object-oriented way to work with strings in Luminova Application.It makes string manipulation cleaner and more expressive.
Creating a String Object
You can create a new Str instance in two ways, directly using the constructor or the of() static helper method.
use Luminova\Components\String\Str;
// Using constructor
$str = new Str('Hello World');
// Using static helper
$str = Str::of('Hello World');Both examples initialize a string object that you can manipulate with class methods.
Optional Array Access
The Str object allows you to access characters by index, just like an array.
$str = Str::of('Hello');
// Access single characters by 0-based index
echo $str[1]; // Outputs: "e"
echo $str[3]; // Outputs: "l"
// Out-of-range indexes return an empty string
echo $str[10]; // Outputs: ""Note:This works best with plain ASCII strings. For multibyte characters (like emojis or non-latin scripts), consider using
at()for safe Unicode support.
Transforming Strings
Once you have an instance, you can perform various transformations easily:
echo $str->toUpperCase(); // "HELLO WORLD"
echo $str->slugify(); // "hello-world"
echo $str->between('He', 'ld'); // "llo Wor"
echo $str->repeat(2); // "Hello WorldHello World"Each method returns a new Str object (unless otherwise noted), allowing method chaining.
Example:
echo Str::of(' Hello World ')
->trim()
->slugify()
->toUpperCase(); // "HELLO-WORLD"Extending with Prototype Methods
You can extend the Str class at runtime using prototype methods.This allows you to dynamically add new methods or utilities without modifying the class itself.
// Add a dynamic method
$str->prototype('greet', fn($name) => "{$this->valueOf}, {$name}!");
echo $str->greet('Peter'); // "Hello World, Peter!"You can also add new utilities:
$str->prototype('toArray', fn() => explode(' ', $this->valueOf));
print_r($str->toArray()); // ["Hello", "World"]Note:Inside a prototype function, use
$this->valueOfto access the string value.The$valueOfproperty is protected and cannot be accessed outside the class scope.
Class Definition
- Class namespace:
Luminova\Components\String\Str - This class implements:
Stringable,ArrayAccess, Luminova\Interface\PrototypeableInterface
This class implements Luminova
PrototypeableInterface, allowing you to dynamically add methods at runtime.
Properties
valueOf
The internal property holding the string value.
protected string $valueOf = '';Encoding Property
Optional string encoding (default: auto detect or fallback to UTF-8).
protected ?string $encoding = null;encodings
Auto encoding detection order.
/**
* @var array<int, string>
*/
protected static array $encodings = [
'UTF-8',
'UTF-16',
'UTF-32',
'ISO-8859-1',
'ISO-8859-15',
'WINDOWS-1252',
'LATIN1',
'ASCII',
'SJIS',
'EUC-JP',
'CP850',
'BIG5'
];Methods
constructor
Create a new string object.
public function __construct(
string $valueOf = '',
?string $encoding = null
)Parameters:
| Parameter | Type | Description |
|---|---|---|
$valueOf | string | The initial string value (default ''). |
$encoding | ?string | Optional string encoding (default: auto detect or fallback to UTF-8). |
Example:
$str = new Str('Hello');of
Create a new Str instance from a string or any Stringable value.
This static factory method makes it easy to initialize the string object without using the new keyword directly.
public static function of(Stringable|string $valueOf, ?string $encoding = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$valueOf | Stringable|string | The string or Stringable object to wrap. |
$encoding | ?string | Optional string encoding (default: auto detect or fallback to UTF-8). |
Return:
self - Returns a new Str instance containing the given value.
Example:
$str = Str::of('Hello World');
// From Object that implements Stringable
$str = Str::of(new SomeStringObject(
'From Another Stringable Class Object'
));fromCharCode
Create a string from one or more Unicode values.
public static function fromCharCode(int ...$codes): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$codes | int | Arguments of unicode code points. |
Return:
self - Returns a new Str instance representing the characters.
Example:
$str = Str::fromCharCode(72, 101, 108, 108, 111);
echo $str; // "Hello"fromCodePoint
Create a string from Unicode code points (like String.fromCodePoint).
public static function fromCodePoint(int ...$points): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$points | int | Arguments of unicode code points. |
Return:
self - Returns a new Str instance representing the characters.
Example:
$str = Str::fromCodePoint(9731, 9733, 9842);
echo $str; // "☃★♲"value
Set or update the current string value.
public function value(Stringable|string $value, ?string $encoding = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$value | Stringable|string | The new string value. |
$encoding | ?string | Optional string encoding (default: auto detect or fallback to UTF-8). |
Return:
self - Returns the same instance for chaining.
Example:
$str = new Str('Old value');
echo $str; // Old Value
// Update with a new value
$str->value('New value');
echo $str; // New Valueat
Get the character at the specified position.
Supports multibyte characters and negative indexes, where -1 represents the last character.Returns an empty string if the index is out of range.
public function at(int $index): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | The zero-based character position (can be negative). |
Return:
self - Returns a new Str instance containing the character, or an empty instance if not found.
Example:
echo Str::of('Hello')->at(1); // "e"
echo Str::of('Hello')->at(-1); // "o"
echo Str::of('你好')->at(1); // "好"
echo Str::of('Hello')->at(99); // ""hash
Hash the string using the given algorithm.
public function hash(string $algo = 'xxh3', bool $binary = false, array $options = []): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$algo | string | The hash algorithm (e.g., md5, sha256) (default 'xxh3'). |
$binary | bool | If true, outputs raw binary data instead of hex (default false). |
$options | array<array-key,mixed> | Optional hashing options. |
Return:
self - Returns the same instance with the hashed value.
Note:This modifies the original string value.
Throws:
- Luminova\Exceptions\InvalidArgumentException - If the algorithm is unsupported.
Example:
echo Str::of('Some Secrete Message')
->hash('sha256')
->toString();length
Calculate length of the string value.
public function length(?string $encoding = null): intParameters:
| Parameter | Type | Description |
|---|---|---|
$encoding | ?string | Character encoding for multibyte support. |
Return:
int - Returns the number of characters in the string.
Example:
echo Str::of('Hello')->length(); // 5encoding
Detect the character encoding of the current string.
This method uses mb_detect_encoding() to guess the encoding from a list of common encodings.If detection fails, it returns the $default value.
If
$strictis true,mb_detect_encodingwill only return a valid encoding if it can verify that the string is valid in that encoding.
public function encoding(bool $strict = true, string $default = 'UTF-8'): stringParameters:
| Parameter | Type | Description |
|---|---|---|
$strict | bool | Whether to use strict mode for detection (default: true). |
$default | string | The fallback encoding to return if detection fails (default: UTF-8). |
Return:
string - Return the detected or default encoding name.
Example:
Str::of("Hello")->encoding();
// 'UTF-8'
Str::of(iconv('UTF-8', 'ISO-8859-1', "Olá"))->encoding();
// 'ISO-8859-1'split
Split the string into chunks of the specified length.
This converts the string value into an array of $length characters.
public function split(int $length = 1): arrayParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | The length of each chunk. |
Return:
string[] - An array of string segments.
Example:
$str = new Str('abcde');
print_r($str->split(2));
// Array("ab", "cd", "e")replace
Replace occurrences of a string or array of strings.
This method uses PHP str_replace to replace one or more occurrence.
public function replace(string|array $search, string|array $replacement): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$search | string|array<array-key,mixed> | The string or array to search for. |
$replacement | string|array<array-key,mixed> | The replacement string or array. |
Return:
self - Returns a new Str instance with replacements applied.
Example:
echo Str::of('Hello World')->replace('World', 'PHP');
// "Hello PHP"
echo Str::of('I like cats and cats')->replace('cats', 'dogs');
// "I like dogs and dogs"replaceAll
Replace occurrences using a regular expression.
This method uses uses regex to find and replace all occurrence.
public function replaceAll(string|array $pattern, string|array $replacement): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$pattern | string|array<array-key,mixed> | A regular expression pattern or array of patterns. |
$replacement | string|array<array-key,mixed> | The replacement string or array. |
Return:
self - Returns a new Str instance with replacements applied.
Example:
echo Str::of('Hello World')->replaceAll('/World/i', 'PHP');
// "Hello PHP"
echo Str::of('I like cats and Cats')->replaceAll('/cats/i', 'dogs');
// "I like dogs and dogs"repeat
Repeat the string a given number of times.
It also support an optional suffix separator for the repeat process.
public function repeat(int $times, string $separator = ''): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$times | int | The number of repetitions. |
$separator | string | An optional repeat suffix separator. |
Return:
self - Returns a new instance with the repeated string.
Example:
$str = new Str('Hi');
echo $str->repeat(3);
// HiHiHi
echo $str->repeat(3, '-');
// Hi-Hi-Hipad
Pad the string to a new length using a given character.
public function pad(int $length, string $char = " ", string $type = 'right'): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$length | int | The target length. |
$char | string | The padding character (default: empty-space). |
$type | string | The padding type (left, right, both). |
Return:
self - Returns a new instance with padding applied.
Example:
$str = new Str('Hi');
echo $str->pad(5, '_');
// Hi___escape
Escape special characters in the string for HTML or attribute context.
public function escape(string $context = 'html', string $encoding = 'UTF-8'): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$context | string | The value escaper content (Either html or attr) (default 'html'). |
$encoding | string | The character encoding (default UTF-8). |
Return:
self - Returns a new instance with escaped content.
Example:
$str = new Str('<b>Unsafe</b>');
echo $str->escape();
// "<b>Unsafe</b>"reverse
Reverse the string.
public function reverse(): selfReturn:
self - Returns a new instance with reversed string.
Example:
$str = new Str('abc');
echo $str->reverse(); // "cba"substring
Extracts a substring from the string using fixed start and optional length.
Unlike slice(), this method does not support negative indexes.It behaves like JavaScript's substring() method — counting only from the beginning of the string.
public function substring(int $start, ?int $length = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$start | int | The starting index (0-based). |
$length | ?int | The number of characters to extract (null for the rest of the string). |
Return:
self - Returns a new instance containing the substring.
Example:
echo Str::of('abcdef')->substring(1, 3); // "bcd"
echo Str::of('abcdef')->substring(2); // "cdef"slice
Extracts a section of the string by start and end indexes.
Supports negative indexes, which count from the end of the string. Works like JavaScript's String.prototype.slice(), more flexible than substring(), since you can use both positive and negative positions.
public function slice(int $start, ?int $end = null): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$start | int | The starting index (can be negative). |
$end | ?int | The ending index (exclusive). Can be negative or null for full length. |
Return:
self - Returns a new Str instance containing the extracted substring.
Example:
echo Str::of('The quick brown fox')->slice(4, 9); // "quick"
echo Str::of('你好世界')->slice(1, 3); // "好世"
echo Str::of('The quick brown fox')->slice(-3); // "fox"
echo Str::of('abcdef')->slice(-4, -1); // "cde"mask
Mask part of the string using a specified character.
public function mask(string $character = '*', string $position = 'center'): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$character | string | The character to use for masking (default '*'). |
$position | string | The mask position (left, right;, center). |
Return:
self - Returns a new instance with the masked string.
Example:
$str = new Str('SensitiveData');
echo $str->mask('*', 'center');
// "S******ata"words
Split the string into individual words.
Non-letter or non-number characters are treated as delimiters.
public function words(): arrayReturn:
string[] - Returns an array of string into words.
Example:
$str = new Str('Hello, world! PHP rocks');
print_r($str->words());
// ["Hello", "world", "PHP", "rocks"]slugify
Convert the string into a URL-friendly slug.
public function slugify(string $delimiter = '-'): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$delimiter | string | The character used to separate words. |
Return:
self - Returns a new instance containing the slug.
Example:
$str = new Str('Hello World!');
echo $str->slugify();
// "hello-world"truncate
Truncate the string to a specified length.
If the string is longer than the limit, it is shortened gracefully.
public function truncate(int $limit): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$limit | int | The maximum string length. |
Return:
self - Returns a new instance with the truncated string.
Example:
$str = new Str('This is too long');
echo $str->truncate(7);
// "This is"wrap
Wrap the string with another string before and/or after.
public function wrap(string $before, string $after): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$before | string | The value to prepend before string value. |
$after | string | The value to append after string value. |
Return:
self - Returns a new instance with wrapped content.
Example:
$str = new Str('text');
echo $str->wrap('<b>', '</b>');
// "<b>text</b>"between
Extract the text between two substrings.
public function between(string $start, string $end): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$start | string | The starting delimiter. |
$end | string | The ending delimiter. |
Return:
self - Returns a new instance containing the extracted text or empty string if not found.
Example:
$str = new Str('Hello [John]');
echo $str->between('[', ']');
// "John"shuffle
Randomly shuffle or select characters from the string.
This method shuffles the string's characters or returns a random subset.If $unique is true, duplicate characters are removed before shuffling.
public function shuffle(?int $length = null, bool $unique = false): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$length | ?int | Optional number of random characters to return. If null, the entire string is randomized (default null). |
$unique | bool | If true, ensures characters do not repeat in the result. |
Return:
self - Returns a new instance containing the randomized string.
Examples:
$str = new Str('abcdefa');
echo $str->shuffle(); // "dfabcea" (varies)
echo $str->shuffle(3); // "bfa" (varies)
echo $str->shuffle(3, true); // "cde" (no repeats)
echo $str->shuffle(unique: true); // "abcdef" (duplicates removed, then shuffled)concat
Concatenate one or more strings to the current string.
public function concat(Stringable|string ...$strings): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$strings | Stringable|string | Arguments of strings or Stringable objects to append. |
Return:
self - Returns a new Str instance containing the concatenated result.
** Example:
$str = Str::of('Hello');
$str2 = Str::of('World');
echo $str->concat(' ', $str2); // "Hello World"equals
Check if the string equals another string.
public function equals(Stringable|string $value, bool $caseSensitive = true): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$value | Stringable|string | The string to compare with. |
$caseSensitive | bool | Compare case-sensitively or not. |
Return:
bool - Returns true if equal, false otherwise.
Example:
$str = new Str('Hello');
echo $str->equals('hello'); // false
echo $str->equals('hello', false); // true (case-insensitive)trim
Trim whitespace or custom characters from both ends.
public function trim(string $characters = " \t\n\r\0\x0B"): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$characters | string | Optional characters to trim. |
Return:
self - Returns a new instance with trimmed string.
Example:
$str = new Str(' hello ');
echo $str->trim();
// "hello"trimStart
Trim from the beginning only.
public function trimStart(string $characters = " \t\n\r\0\x0B"): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$characters | string | Optional characters to trim. |
Return:
self - Returns a new instance with trimmed start.
Example:
echo Str::of(' hello ')->trimStart(); // "hello "trimEnd
Trim from the end only.
public function trimEnd(string $characters = " \t\n\r\0\x0B"): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$characters | string | Optional characters to trim. |
Return:
self - Returns a new instance with trimmed end.
Example:
echo Str::of(' hello ')->trimEnd(); // " hello"contains
Check if the string contains another string.
Alias: includes()
public function contains(Stringable|string $needle, bool $caseSensitive = true): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$needle | Stringable|string | The substring to find. |
$caseSensitive | bool | Whether to match case-sensitively. |
Return:
bool - Return true if found, false otherwise.
Example:
if(Str::of('Hello')->contains('ell')){
echo 'Match found';
}
// Or with alias
if(Str::of('Hello')->includes('ell')){
echo 'Match found';
}matches
Test if the string matches a regex pattern.
public function matches(string $pattern): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$pattern | string | The Regular expression to match. |
Return:
bool - Return true if it matches, false otherwise.
Example:
if(Str::of('abc123')->matches('/\d+/')){
echo 'Match found';
}search
Find the position of the first match for a regex pattern.
public function search(string $pattern): intParameters:
| Parameter | Type | Description |
|---|---|---|
$pattern | string | Regular expression to search for. |
Return:
int - Returns the index of the first match, or -1 if not found.
Example:
Works like JavaScript's String.prototype.search().
$str = Str::of("I think Ruth's dog is cuter than your dog!");
$pos = $str->search('/[^\w\s\']/'); // 41
echo $str[$pos]; // "!"position
Find the position of a substring.
Retrieves the position of the first occurrence of a substring within the string.
public function position(Stringable|string $needle, int $offset = 0, bool $caseSensitive = true): int|boolParameters:
| Parameter | Type | Description |
|---|---|---|
$needle | Stringable|string | The substring to find within the string. |
$offset | int | The position to start searching from (default: 0). |
$caseSensitive | bool | Whether the search should be case-sensitive (default: true). |
Return:
int|bool - Returns the position of the first match, or false if not found.
Example:
$str = Str::of('Hello World');
echo $str->position('World'); // 6
echo $str->position('world', caseSensitive: false); // 6 (case-insensitive)
echo $str->position('PHP'); // falseThis method works like PHP's native
strpos(), but allows optional case-insensitive search when$caseSensitiveis set tofalse.
normalize
Normalize the string to a specified Unicode form.
This method ensures that visually identical Unicode strings are representedconsistently in memory (useful for comparisons, hashing, or storage).
public function normalize(string $form = 'NFC'): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$form | string | The normalization form (Default: NFC).Accepted values: NFC, NFD, NFKC, NFKD. |
Return:
self - Returns a new normalized string instance.
Throws:
- Luminova\Exceptions\RuntimeException - If intl extension is not available.
Example:
// Characters with combining accents may look identical but differ in code units
$str = new Str("e\u{0301}"); // "e" + "́"
echo $str->normalize('NFC')->toString(); // "é"Note:Requires the PHP
intlextension.
toLowerCase
Transform the string value to lowercase.
public function toLowerCase(): selfReturn:
self - Returns a new instance with lowercase text.
Example:
echo Str::of('Hello World')->toUpperCase(); // hello worldtoUpperCase
Transform the string value to uppercase.
public function toUpperCase(): selfReturn:
self - Returns a new instance with uppercase text.
Example:
echo Str::of('Hello World')->toUpperCase(); // HELLO WORLDtoCamelCase
Transform the string value to camelCase.
public function toCamelCase(): selfReturn:
self - Returns a new instance in camelCase format.
Example:
echo Str::of('Hello World')->toCamelCase(); // helloWorldtoPascalCase
Transform the string value to PascalCase.
public function toPascalCase(): selfReturn:
self - Returns a new instance in PascalCase format.
Example:
echo Str::of('Hello World')->toPascalCase(); // HelloWorldtoSnakeCase
Transform the string value to snake_case.
public function toSnakeCase(): selfReturn:
self - Returns a new instance in snake_case format.
Example:
echo Str::of('Hello World')->toSnakeCase(); // Hello_WorldtoKebabCase
Transform the string value to kebab-case.
public function toKebabCase(bool $toLowerCase = false): selfParameters:
| Parameter | Type | Description |
|---|---|---|
$toLowerCase | bool | Whether to force lowercase. |
Return:
self - Returns a new instance in kebab-case format.
Example:
echo Str::of('Hello World')->toKebabCase(); // Hello-WorldtoWellFormed
Ensure the string is well-formed UTF-8.
Invalid byte sequences will be replaced with the Unicode replacement character (U+FFFD).
public function toWellFormed(): selfReturn:
self - A new well-formed string instance.
Example:
echo Str::of("Hello\x80World")->toWellFormed(); // "Hello�World"toEncoding
Convert the string to a specific encoding.
public function toEncoding(string $toEncoding, ?string $fromEncoding = null): staticParameters:
| Parameter | Type | Description |
|---|---|---|
$toEncoding | string | The target encoding (e.g., UTF-8, ISO-8859-1). |
$fromEncoding | ?string | Optional source encoding. If null, detects automatically. |
Return:
static - Returns a new Str instance in the target encoding.
Example:
echo Str::of('Héllo 🌍')->toEncoding('ISO-8859-1'); // "H?llo ?"
echo Str::of('Héllo 🌍')->toEncoding('UTF-8'); // "Héllo 🌍"toString
Get the transformed string value.
public function toString(): stringReturn:
string - Return the final value of string object.
Example:
echo Str::of('Hello')->toString(); // "Hello"endsWith
Check if the string ends with a specific substring.
If $position is provided, the string is treated as if it were only that long.
public function endsWith(
Stringable|string $needle,
?int $position = null,
bool $caseSensitive = true
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$needle | Stringable|string | The substring to check. |
$position | ?int | The end position at which is expected to be found (default: null string length). |
$caseSensitive | bool | Whether to match case-sensitively (default: true). |
Return:
bool - Returns true if the string ends with the given substring.
Examples:
if(Str::of('Hello')->endsWith('o')){
echo 'Value ends with o';
}
// More examples
Str::of("Hello world")->endsWith("world"); // true
Str::of("Hello world")->endsWith("World", caseSensitive: false); // true (case-insensitive)
Str::of("Hello world")->endsWith("Hello", 5); // true (checks "Hello")startsWith
Check if the string starts with a specific substring.
public function startsWith(
Stringable|string $needle,
?int $position = null,
bool $caseSensitive = true
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$needle | Stringable|string | The substring to check. |
$position | ?int | The start position at which is expected to be found (default: null 0). |
$caseSensitive | bool | Whether to match case-sensitively (default: true). |
Return:
bool - Returns true if the string starts with the given substring.
Example:
if(Str::of('Hello')->startsWith('H')){
echo 'Value starts with H';
}
// More examples
Str::of("Hello world")->startsWith("Hello"); // true
Str::of("Hello world")->startsWith("hello", caseSensitive: false); // true (case-insensitive)
Str::of("Hello world")->startsWith("world", 6); // true (starts at index 6)charCodeAt
Get the UTF-16 code unite of the character at a specific index.
public function charCodeAt(int $index): ?intParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | The character position (0-based). |
Return:
int|null - Returns the 16-bit code unit or null if out of range
Example:
echo Str::of("☃★♲")->charCodeAt(1); // 9733 (★)
echo Str::of("😀")->charCodeAt(0); // 55357 (high surrogate)
echo Str::of("😀")->charCodeAt(1); // 56832 (low surrogate)codePointAt
Get the Unicode code point at the given position.
This differs from charCodeAt() in that it returns the full Unicode code point, even for surrogate pairs (e.g., emojis or rare symbols).
public function codePointAt(int $index): ?intParameters:
| Parameter | Type | Description |
|---|---|---|
$index | int | The position of the character. |
Return:
int|null - Returns the Unicode code point, or null if out of range.
Example:
echo Str::of('A')->codePointAt(0); // 65
echo Str::of('你')->codePointAt(0); // 20320
echo Str::of('😀')->codePointAt(0); // 128512isWellFormed
Check whether the string is a well-formed UTF-8 sequence.
This method verifies that all bytes in the string are valid UTF-8 characters.It does not modify the string — it only checks for encoding validity.
public function isWellFormed(): boolReturn:
bool - Returns true if the string is a valid UTF-8 sequence, or false if it contains invalid or malformed bytes.
Example:
echo Str::of('Hello')->isWellFormed(); // true
echo Str::of("Hello\xC0World")->isWellFormed(); // falseisEmpty
Check if the string is empty.
public function isEmpty(): boolReturn:
bool - Returns true if empty, false otherwise.
Example:
if(Str::of('Hello')->isEmpty()){
echo 'Is empty value';
}isNumeric
Check if the string is numeric.
public function isNumeric(): boolReturn:
bool - Returns true if numeric, false otherwise.
Example:
if(Str::of('1234567')->isNumeric()){
echo 'Is Numeric value';
}isHex
Check if value is a hexadecimal representation.
public function isHex(): boolReturn:
bool - Return true if value is a hexadecimal representation.
Example:
if(Str::of('1234567')->isHex()){
echo 'Is Hexadecimal value';
}includes
Alias for contains().
public function includes(Stringable|string $needle, bool $caseSensitive = true): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$needle | Stringable|string | The substring to find. |
$caseSensitive | bool | Whether to match case-sensitively (default: true). |
Return:
bool - Return true if found, false otherwise.
offsetExists
Check whether an index refers to a character.
public function offsetExists(mixed $offset): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | Index to test. |
Return:
bool - Returns true for an integer index from zero to one less than the string length.
offsetGet
Read a character using the object’s configured encoding.
public function offsetGet(mixed $offset): ?stringParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | int | Character index. |
Return:
string|null - Returns the character, or null if the integer index is outside the string.
Throws:
Luminova\Exceptions\InvalidArgumentException- If the offset is not an integer.
offsetSet
Replace the character at an index with the string form of the supplied value.
public function offsetSet(mixed $offset, mixed $value): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | int | Character index. |
$value | mixed | Replacement converted to a string. |
Throws:
Luminova\Exceptions\InvalidArgumentException- If the offset is not an integer.
offsetUnset
Remove the character at an index.
public function offsetUnset(mixed $offset): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | int | Character index. |
Throws:
Luminova\Exceptions\InvalidArgumentException- If the offset is not an integer.
assert
Validate the type of a string offset.
public function assert(mixed $offset): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | mixed | Offset to validate. |
Throws:
Luminova\Exceptions\InvalidArgumentException- If the offset is not an integer.