romanlazko / str
String helper functions.
1.3
2025-05-24 12:01 UTC
Requires
- php: >=8.0
- romanlazko/support-macroable: >=1.0
This package is auto-updated.
Last update: 2025-05-24 12:01:46 UTC
README
A comprehensive string manipulation library for PHP with multibyte support.
Installation
composer require romanlazko/str
Usage
Using the Str Class
use RomanLazko\Str\Str; // Get string length $length = Str::length('Hello World'); // 11 // Convert to kebab-case $kebab = Str::kebab('fooBar'); // 'foo-bar' // Convert to snake_case $snake = Str::snake('fooBar'); // 'foo_bar' // Convert to camelCase $camel = Str::camel('foo-bar'); // 'fooBar' // Convert to StudlyCase $studly = Str::studly('foo_bar'); // 'FooBar' // String manipulation $reversed = Str::reverse('Hello'); // 'olleH' $limited = Str::limit('This is a long string', 10); // 'This is a...' $words = Str::words('This is a long string', 3); // 'This is a...'
Using the str() Helper
// Chainable string manipulation $result = str('Hello World') ->upper() ->replace('WORLD', 'Universe') ->finish('!') ->toString(); // Result: 'HELLO UNIVERSE!'
Available Methods
String Case Conversion
camel(string $string): string
- Convert to camelCasekebab(string $string): string
- Convert to kebab-casesnake(string $string, string $delimiter = '_'): string
- Convert to snake_casestudly(string $string): string
- Convert to StudlyCasetitle(string $string): string
- Convert to Title Caseucfirst(string $string): string
- Make first character uppercaselcfirst(string $string): string
- Make first character lowercaseupper(string $string): string
- Convert to uppercaselower(string $string): string
- Convert to lowercase
String Manipulation
length(string $string, ?string $encoding = 'UTF-8'): int
- Get string lengthlimit(string $string, int $limit = 100, string $end = '...'): string
- Limit string lengthwords(string $string, int $words = 100, string $end = '...'): string
- Limit number of wordsreverse(string $string): string
- Reverse the stringreplace(string $string, string|array $search, string|array $replace, int $count = null): string
- Replace textrepeat(string $string, int $times): string
- Repeat stringslug(string $string, string $separator = '-'): string
- Generate URL-friendly slugsubstr(string $string, int $start, ?int $length = null, string $encoding = 'UTF-8'): string
- Get substringsubstrCount(string $string, string $needle): int
- Count substring occurrencessubstrReplace(string $string, string $replace, int $offset = 0, ?int $length = null): string
- Replace text within portion of stringtrim(string $string, string $charlist = " \t\n\r\v\0"): string
- Strip whitespace from beginning and endltrim(string $string, string $charlist = " \t\n\r\v\0"): string
- Strip whitespace from beginningrtrim(string $string, string $charlist = " \t\n\r\v\0"): string
- Strip whitespace from endstart(string $string, string $cap): string
- Add string to start if not presentfinish(string $string, string $cap): string
- Add string to end if not present
String Checks
contains(string $string, string $needle): bool
- Check if string contains substringis(string $pattern, string $value): bool
- Check if string matches patternstartsWith(string $string, string $needle): bool
- Check if string starts with substringendsWith(string $string, string $needle): bool
- Check if string ends with substringisAscii(string $string): bool
- Check if string is ASCIIisUuid(string $string): bool
- Check if string is a valid UUID
Helper Methods
str($string = null)
- Create a chainable string instance$result = str('hello')->upper()->finish('!'); // 'HELLO!'
License
This package is open-source software licensed under the MIT license.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.