toolkit

Str

String helpers: case, slug, search, replace, split and join.

Unicode-aware through mb_*. Comparisons (contains, startsWith, replace and the like) ignore case unless their last argument says otherwise.

Str:: Phlat\Toolkit\Str 47 methods

Inspection 7

  • length Unicode-aware character count.
  • isEmpty Whether the string is empty.
  • isDigits Whether the string is one or more digits and nothing else.
  • startsWith Whether the string starts with $prefix, ignoring case unless told otherwise.
  • endsWith Whether the string ends with $suffix, ignoring case unless told otherwise.
  • contains Whether the string contains $needle, ignoring case unless told otherwise.
  • count How many times $needle occurs in $string, without overlaps.

Extraction 6

  • before Returns the full string if needle is not found.
  • after Returns the full string if needle is not found.
  • between The text between $start and $end.
  • beforeLast Returns the full string if needle is not found.
  • afterLast Returns the full string if needle is not found.
  • substr Returns a substring.

Transformation 13

  • lower Unicode-aware lowercase.
  • upper Unicode-aware uppercase.
  • base64 Standard base64, for data: URIs and the like.
  • ucFirst Uppercase first character only, leave the rest unchanged.
  • title Unicode-aware title case.
  • slug ASCII only: transliterates Latin letters, then collapses every run of anything but a-z and 0-9 to the separator and trims it.
  • asciiSlug slug() for use as an external identifier (e.g. a search index document id): falls back to a hash for values with nothing Latin to keep (CJK, Cyrillic, etc).
  • camel Converts hyphen/underscore/space separated strings to camelCase.
  • snake Converts camelCase to snake_case.
  • truncate Truncates to $length characters.
  • wordCount The number of words in $string.
  • words Limits to $count words, appending $suffix if truncated.
  • repeat The string repeated $times times.

Removal 5

  • trim Strips $characters from both ends.
  • trimStart Strips $characters from the start.
  • trimEnd Strips $characters from the end.
  • removeStart Removes $prefix once when the string starts with it.
  • removeEnd Removes $suffix once when the string ends with it.

Replacement 6

  • replace Replaces every occurrence, ignoring case unless told otherwise.
  • replaceFirst Replaces only the first occurrence.
  • replaceLast Replaces only the last occurrence.
  • replaceRegex Replaces regex matches, as preg_replace().
  • replaceRegexUsing Replaces each match of $pattern with what $callback returns for its matches array.
  • translate Replaces multiple substrings using a [search => replace] map.

Splitting 4

  • split Splits on $delimiter into at most $limit parts.
  • splitOutside Splits on a one-character delimiter that sits outside "quotes" and (parentheses), so `text="a, b",bg=rgb(0, 0, 0)` is two parts.
  • join Joins the parts with $glue.
  • lines Splits on both \n and \r\n line endings.

Comparison 2

  • distance Levenshtein edit distance between two strings - number of single-character edits to turn one into the other.
  • suggest The closest of $options to $input within a typo's reach (about a third of its length), or null.

Pattern 4

  • match Returns true if $pattern matches $subject, populating $matches if provided.
  • matchAll Returns true if $pattern matches anywhere in $subject, populating $matches (set order) if provided.
  • quote Escapes $string for use inside a regex whose delimiter is $delimiter.
  • className The fully qualified class name of $object.

Inspection

Str::length(string $string): int

Unicode-aware character count.

Parameters (1)
$string string

Returns int

phlat/src/Toolkit/Str.php:30
Str::isEmpty(string $string): bool

Whether the string is empty.

Parameters (1)
$string string

Returns bool

phlat/src/Toolkit/Str.php:36
Str::isDigits(string $string): bool

Whether the string is one or more digits and nothing else.

Parameters (1)
$string string

Returns bool

phlat/src/Toolkit/Str.php:42
Str::startsWith(string $string, string $prefix, bool $caseInsensitive = true): bool

Whether the string starts with $prefix, ignoring case unless told otherwise.

Parameters (3)
$string string
$prefix string
$caseInsensitive bool = true

Returns bool

phlat/src/Toolkit/Str.php:48
Str::endsWith(string $string, string $suffix, bool $caseInsensitive = true): bool

Whether the string ends with $suffix, ignoring case unless told otherwise.

Parameters (3)
$string string
$suffix string
$caseInsensitive bool = true

Returns bool

phlat/src/Toolkit/Str.php:57
Str::contains(string $string, string $needle, bool $caseInsensitive = true): bool

Whether the string contains $needle, ignoring case unless told otherwise.

Parameters (3)
$string string
$needle string
$caseInsensitive bool = true

Returns bool

phlat/src/Toolkit/Str.php:66
Str::count(string $string, string $needle): int

How many times $needle occurs in $string, without overlaps.

Parameters (2)
$string string
$needle string

Returns int

phlat/src/Toolkit/Str.php:75

Extraction

Str::before(string $string, string $needle, bool $caseInsensitive = true): string

Returns the full string if needle is not found.

Parameters (3)
$string string
$needle string
$caseInsensitive bool = true

Returns string

phlat/src/Toolkit/Str.php:83
Str::after(string $string, string $needle, bool $caseInsensitive = true): string

Returns the full string if needle is not found.

Parameters (3)
$string string
$needle string
$caseInsensitive bool = true

Returns string

phlat/src/Toolkit/Str.php:90
Str::between(string $string, string $start, string $end): string

The text between $start and $end.

Parameters (3)
$string string
$start string
$end string

Returns string

phlat/src/Toolkit/Str.php:97
Str::beforeLast(string $string, string $needle, bool $caseInsensitive = true): string

Returns the full string if needle is not found.

Parameters (3)
$string string
$needle string
$caseInsensitive bool = true

Returns string

phlat/src/Toolkit/Str.php:103
Str::afterLast(string $string, string $needle, bool $caseInsensitive = true): string

Returns the full string if needle is not found.

Parameters (3)
$string string
$needle string
$caseInsensitive bool = true

Returns string

phlat/src/Toolkit/Str.php:111
Str::substr(string $string, int $start, ?int $length = null): string

Returns a substring.

Negative $start counts from the end.

Parameters (3)
$string string
$start int
$length ?int = null

Returns string

phlat/src/Toolkit/Str.php:119

Transformation

Str::lower(string $string): string

Unicode-aware lowercase.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:127
Str::upper(string $string): string

Unicode-aware uppercase.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:133
Str::base64(string $string): string

Standard base64, for data: URIs and the like.

Url::base64Encode() is the URL-safe form.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:139
Str::ucFirst(string $string): string

Uppercase first character only, leave the rest unchanged.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:145
Str::title(string $string): string

Unicode-aware title case.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:151
Str::slug(string $string, string $separator = '-'): string

ASCII only: transliterates Latin letters, then collapses every run of anything but a-z and 0-9 to the separator and trims it.

_ is never legal in a URL path (a leading _ on disk marks a collection container). A name with nothing Latin left (CJK, Cyrillic) comes out empty, and the caller rejects it.

Str::slug('Crème Brûlée, 2 ways!'); // 'creme-brulee-2-ways'
Str::slug('Hello World', '_');      // 'hello_world'
Parameters (2)
$string string
$separator string = '-'

Returns string

phlat/src/Toolkit/Str.php:164
Str::asciiSlug(string $string): string

slug() for use as an external identifier (e.g. a search index document id): falls back to a hash for values with nothing Latin to keep (CJK, Cyrillic, etc).

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:172
Str::camel(string $string): string

Converts hyphen/underscore/space separated strings to camelCase.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:192
Str::snake(string $string): string

Converts camelCase to snake_case.

Parameters (1)
$string string

Returns string

phlat/src/Toolkit/Str.php:198
Str::truncate(string $string, int $length, string $suffix = '...'): string

Truncates to $length characters.

The suffix counts toward the limit.

Parameters (3)
$string string
$length int
$suffix string = '...'

Returns string

phlat/src/Toolkit/Str.php:204
Str::wordCount(string $string): int

The number of words in $string.

Parameters (1)
$string string

Returns int

phlat/src/Toolkit/Str.php:213
Str::words(string $string, int $count, string $suffix = '...'): string

Limits to $count words, appending $suffix if truncated.

Parameters (3)
$string string
$count int
$suffix string = '...'

Returns string

phlat/src/Toolkit/Str.php:219
Str::repeat(string $string, int $times): string

The string repeated $times times.

Parameters (2)
$string string
$times int

Returns string

phlat/src/Toolkit/Str.php:229

Removal

Str::trim(string $string, string $characters = ' \n\r\t\v\0'): string

Strips $characters from both ends.

Parameters (2)
$string string
$characters string = ' \n\r\t\v\0'

Returns string

phlat/src/Toolkit/Str.php:237
Str::trimStart(string $string, string $characters = ' \n\r\t\v\0'): string

Strips $characters from the start.

Parameters (2)
$string string
$characters string = ' \n\r\t\v\0'

Returns string

phlat/src/Toolkit/Str.php:243
Str::trimEnd(string $string, string $characters = ' \n\r\t\v\0'): string

Strips $characters from the end.

Parameters (2)
$string string
$characters string = ' \n\r\t\v\0'

Returns string

phlat/src/Toolkit/Str.php:249
Str::removeStart(string $string, string $prefix): string

Removes $prefix once when the string starts with it.

Parameters (2)
$string string
$prefix string

Returns string

phlat/src/Toolkit/Str.php:255
Str::removeEnd(string $string, string $suffix): string

Removes $suffix once when the string ends with it.

Parameters (2)
$string string
$suffix string

Returns string

phlat/src/Toolkit/Str.php:261

Replacement

Str::replace(string $string, array|string $search, array|string $replace, bool $caseInsensitive = true): string

Replaces every occurrence, ignoring case unless told otherwise.

Parameters (4)
$string string
$search array|string
$replace array|string
$caseInsensitive bool = true

Returns string

phlat/src/Toolkit/Str.php:269
Str::replaceFirst(string $string, string $search, string $replace): string

Replaces only the first occurrence.

Parameters (3)
$string string
$search string
$replace string

Returns string

phlat/src/Toolkit/Str.php:278
Str::replaceLast(string $string, string $search, string $replace): string

Replaces only the last occurrence.

Parameters (3)
$string string
$search string
$replace string

Returns string

phlat/src/Toolkit/Str.php:285
Str::replaceRegex(array|string $pattern, array|string $replacement, array|string $subject): array|string

Replaces regex matches, as preg_replace().

Parameters (3)
$pattern array|string
$replacement array|string
$subject array|string

Returns array|string

phlat/src/Toolkit/Str.php:292
Str::replaceRegexUsing(string $pattern, callable $callback, string $subject): string

Replaces each match of $pattern with what $callback returns for its matches array.

Parameters (3)
$pattern string
$callback callable
$subject string

Returns string

phlat/src/Toolkit/Str.php:298
Str::translate(string $string, array $map): string

Replaces multiple substrings using a [search => replace] map.

Parameters (2)
$string string
$map array

Returns string

phlat/src/Toolkit/Str.php:304

Splitting

Str::split(string $string, string $delimiter, int $limit = PHP_INT_MAX): array

Splits on $delimiter into at most $limit parts.

Parameters (3)
$string string
$delimiter string
$limit int = PHP_INT_MAX

Returns array

phlat/src/Toolkit/Str.php:312
Str::splitOutside(string $string, string $delimiter): array

Splits on a one-character delimiter that sits outside "quotes" and (parentheses), so `text="a, b",bg=rgb(0, 0, 0)` is two parts.

Parameters (2)
$string string
$delimiter string

Returns array

phlat/src/Toolkit/Str.php:318
Str::join(array $parts, string $glue): string

Joins the parts with $glue.

Parameters (2)
$parts array
$glue string

Returns string

phlat/src/Toolkit/Str.php:343
Str::lines(string $string): array

Splits on both \n and \r\n line endings.

Parameters (1)
$string string

Returns array

phlat/src/Toolkit/Str.php:349

Comparison

Str::distance(string $a, string $b): int

Levenshtein edit distance between two strings - number of single-character edits to turn one into the other.

Parameters (2)
$a string
$b string

Returns int

phlat/src/Toolkit/Str.php:357
Str::suggest(string $input, array $options): ?string

The closest of $options to $input within a typo's reach (about a third of its length), or null.

Parameters (2)
$input string
$options array

Returns ?string

phlat/src/Toolkit/Str.php:363

Pattern

Str::match(string $pattern, string $subject, ?array $matches = null): bool

Returns true if $pattern matches $subject, populating $matches if provided.

Parameters (3)
$pattern string
$subject string
$matches ?array = null

Returns bool

phlat/src/Toolkit/Str.php:380
Str::matchAll(string $pattern, string $subject, ?array $matches = null): bool

Returns true if $pattern matches anywhere in $subject, populating $matches (set order) if provided.

Parameters (3)
$pattern string
$subject string
$matches ?array = null

Returns bool

phlat/src/Toolkit/Str.php:386
Str::quote(string $string, string $delimiter = '~'): string

Escapes $string for use inside a regex whose delimiter is $delimiter.

Parameters (2)
$string string
$delimiter string = '~'

Returns string

phlat/src/Toolkit/Str.php:392
Str::className(object $object): string

The fully qualified class name of $object.

Parameters (1)
$object object

Returns string

phlat/src/Toolkit/Str.php:398