Skip to content

📘 Str — Helper Class for String Manipulation

The Str class provides commonly used methods for string operations.

🔹 Str::after(string $subject, string $search): string

Returns the part of the string after the first occurrence of the given substring.

php
Str::after('user@example.com', '@'); // 'example.com'

🔹 Str::before(string $subject, string $search): string

Returns the part of the string before the first occurrence of the given substring.

php
Str::before('user@example.com', '@'); // 'user'

🔹 Str::camel(string $string): string

Converts a string to camelCase.

php
Str::camel('page_blocks'); // 'pageBlocks'

🔹 Str::contains(string $haystack, string $needle): bool

Checks if a string contains the given substring.

php
Str::contains('framework', 'work'); // true

🔹 Str::endsWith(string $haystack, string $needle): bool

Checks if a string ends with the given substring.

php
Str::endsWith('index.php', '.php'); // true

🔹 Str::explode(string $delimiter, string $string, ?int $limit = null, $padValue = null): array

Splits a string by delimiter into an array. If a limit is given, the result will always have $limit elements, padded with $padValue if needed.

php
Str::explode(',', 'one,two'); 
// ['one', 'two']

Str::explode(',', 'one,two', 4, ''); 
// ['one', 'two', '', '']

🔹 Str::finish(string $value, string $cap): string

Appends a string if it’s not already at the end.

php
Str::finish('path/to/dir', '/'); // 'path/to/dir/'

🔹 Str::isAscii(string $string): bool

Checks if a string contains only ASCII characters.

php
Str::isAscii('Hello'); // true
Str::isAscii('Привет'); // false

🔹 Str::isJson(string $string): bool

Checks if a string is a valid JSON.

php
Str::isJson('{"a":1}'); // true
Str::isJson('{a:1}'); // false

🔹 Str::isStringable($value): bool

Checks if a value can be cast to a string (string, int, float, bool, or object with __toString()).

php
Str::isStringable(123); // true
Str::isStringable(new DateTime()); // true
Str::isStringable([]); // false

🔹 Str::kebab(string $string): string

Converts a string to kebab-case.

php
Str::kebab('PageBlocks Helper'); // 'page-blocks-helper'

🔹 Str::length(string $string): int

Returns the length of a string (UTF-8 safe).

php
Str::length('Привет'); // 6

🔹 Str::limit(string $string, int $limit = 100, string $end = '...'): string

Limits a string to a certain length and appends the given ending if truncated.

php
Str::limit('This is a long sentence.', 10); // 'This is a ...'

🔹 Str::mask(string $string, string $char = '*', int $index = 0, ?int $length = null): string

Replaces part of the string with a masking character.

php
Str::mask('secret@example.com', '*', 0, 6); // '******@example.com'

🔹 Str::match(string $pattern, string $subject): ?string

Returns the first match from a regex.

php
Str::match('/\d+/', 'Order #12345'); // '12345'

🔹 Str::password(int $length = 16): string

Generates a secure random password.

php
Str::password(12); // 't8K!sB0@qZ4#'

🔹 Str::position(string $haystack, string $needle): ?int

Returns the position of a substring or null.

php
Str::position('example@test.com', '@'); // 7

🔹 Str::random(int $length = 16): string

Generates a random alphanumeric string.

php
Str::random(8); // 'a8Gf3HkL'

🔹 Str::remove(string $string, string $search): string

Removes all occurrences of a substring.

php
Str::remove('this is a test', 'is'); // 'th  a test'

🔹 Str::replace(string $search, string $replace, string $subject): string

Replaces all occurrences of a substring.

php
Str::replace('world', 'PageBlocks', 'Hello world'); // 'Hello PageBlocks'

🔹 Str::reverse(string $string): string

Reverses a string (UTF-8 safe).

php
Str::reverse('Привет'); // 'тевирП'

🔹 Str::slug(string $string, string $separator = '-'): string

Converts a string to a URL-friendly slug.

php
Str::slug('Пример строки для URL!'); // 'primer-stroki-dlya-url'
Str::slug('Page Blocks Helper', '_'); // 'page_blocks_helper'

🔹 Str::snake(string $string): string

Converts a string to snake_case.

php
Str::snake('PageBlocks'); // 'page_blocks'

🔹 Str::start(string $value, string $prefix): string

Prepends a string if it’s not already there.

php
Str::start('example.com', 'https://'); // 'https://example.com'

🔹 Str::startsWith(string $haystack, string $needle): bool

Checks if a string starts with the given substring.

php
Str::startsWith('hello world', 'hello'); // true

🔹 Str::ulid(): string

Generates a ULID (sortable unique ID).

php
Str::ulid(); // '01HXY56Y2B3A9Z2Q7GJ6RFDQXY'

🔹 Str::uuid(): string

Generates a UUID v4.

php
Str::uuid(); // 'b1b0c210-4cda-4d65-9e3b-0f63ff8c0a58'

🔹 Str::wordCount(string $string): int

Counts the number of words in a string.

php
Str::wordCount('This is some text'); // 4

🔹 Str::words(string $string, int $words = 100, string $end = '...'): string

Limits a string to the given number of words.

php
Str::words('This is a test of word limiting', 4); // 'This is a test...'

© PageBlocks 2019-present