myerscode/utilities-strings
A small PHP utility library providing string helper functions for common formatting and manipulation tasks. Useful for Laravel or plain PHP projects to reduce boilerplate for trimming, case conversion, searching, and other everyday string operations.
UtilityGet the remainder of the string after the first occurrence of a given value
$str = new Utility('Hello World');
echo $str->after('Hello ');
// World
echo $str->after('xyz');
// (empty string)
echo $str->after('');
// Hello World
UtilityGet the remainder of the string after the last occurrence of a given value
$str = new Utility('foo.bar.baz');
echo $str->afterLast('.');
// baz
echo $str->afterLast('xyz');
// (empty string)
echo $str->afterLast('');
// foo.bar.baz
UtilityAppend a value to the string
$str = new Utility('Hello World');
echo $str->append('!');
// Hello World!
stringGet the character at a specific index
$str = new Utility('Hello World');
echo $str->at(6);
// W
UtilityGet the portion of the string before the first occurrence of a given value
$str = new Utility('Hello World');
echo $str->before(' World');
// Hello
echo $str->before('xyz');
// (empty string)
echo $str->before('');
// Hello World
UtilityGet the portion of the string before the last occurrence of a given value
$str = new Utility('foo.bar.baz');
echo $str->beforeLast('.');
// foo.bar
echo $str->beforeLast('xyz');
// (empty string)
echo $str->beforeLast('');
// foo.bar.baz
boolBeginsWith defaults to case insensitive checks.
$str = new Utility('Hello World');
echo $str->beginsWith('hello');
// true
echo $str->beginsWith('hello', true);
// false
UtilityGet the portion of the string between two given values
$str = new Utility('Hello World!');
echo $str->between('Hello ', '!');
// World
$str = new Utility('key=value&other');
echo $str->between('=', '&');
// value
UtilityClean will do a basic trim and strip_tags calls on the string.
You can pass an optional $allowable_tags parameter to do define tags that the underlying strip_tags will preserve.
$str = new Utility('<p>Hello <strong>World</strong></p>');
echo $str->clean();
// Hello World
echo $str->beginsWith('<p>Hello <strong>World</strong></p>', '<p>');
// <p>Hello World</p>
boolCheck if the string contains a given value.
Has an optional $offset parameter to change where the string starts looking from.
$str = new Utility('Hello World');
echo $str->contains('World');
// true
echo $str->contains('xyz');
// false
echo $str->contains('World', 10);
// false
boolWill check if the string contains ALL the passed values.
Has an optional $offset parameter to change where the string starts looking from.
$str = new Utility('Hello World. Foo Bar')
echo $str->containsAll('Foo');
// true
echo $str->containsAll('Food');
// false
echo $str->containsAll(['Foo', 'Bar']);
// true
echo $str->containsAll(['Food', 'Bar']);
// false
echo $str->containsAll(['Hello', 'Bar']);
// true
echo $str->containsAll(['Hello', 'World'], 20);
// false
boolWill check if the string contains ANY of the passed values. The method turns as soon as one value is found.
Has an optional $offset parameter to change where the string starts looking from.
$str = new Utility('Hello World. Foo Bar')
echo $str->containsAny('Foo');
// true
echo $str->containsAny('Food');
// false
echo $str->containsAny(['Food', 'Bar']);
// true
echo $str->containsAny(['Hello', 'World'], 20);
// false
boolCheck to see if the string ends with any of the given values
Has an optional $strict parameter to use case sensitivity or not, defaults to false.
$str = new Utility('Hello World. Foo Bar')
echo $str->endsWith('Foo');
// false
echo $str->endsWith('Bar');
// true
echo $str->endsWith(['Hello World', 'Foo Bar']);
// true
echo $str->endsWith(['Hello World', 'Foo']);
// false
UtilityCheck if the string beings with the given value, if not prepends it.
$str = new Utility('Foo Bar')
echo $str->ensureBeingsWith('Foo');
// Foo Bar
echo $str->ensureBeingsWith('Hello ');
// Hello Foo Bar
UtilityCheck if the string ends with the given value, if not appends it.
$str = new Utility('Foo Bar')
echo $str->ensureEndsWith('Bar');
// Foo Bar
echo $str->ensureEndsWith(' World');
// Foo Bar World
boolCompare the string with another
$str = new Utility('Foo Bar')
echo $str->equals('Foo Bar');
// true
echo $str->ensureEndsWith('foo bar');
// false
arrayExplode the string by a delimiter, trimming and removing any empty values from the results
$str = new Utility('Foo, Bar');
echo $str->explode(',');
// ['Foo', 'Bar']
$str = new Utility('Foo,,,,Bar');
echo $str->explode(',');
// ['Foo', 'Bar']
boolReplace placeholders with the given values in order
$str = new Utility('Hello {0} Foo {1}')
echo $str->format('World', 'Bar);
// Hello World Foo Bar
$str = new Utility('{0} {1} {0} {1} {0} {1}')
echo $str->format('Foo', 'Bar');
// Foo Bar Foo Bar Foo Bar
boolCheck if the string only contains alphanumeric characters
$str = new Utility('Foo Bar 123')
echo $str->isAlphanumeric();
// true
$str = new Utility('Foo Bar!!!')
echo $str->isAlphanumeric();
// false
boolCheck if the string only contains alpha characters
$str = new Utility('FooBar')
echo $str->isAlpha();
// true
$str = new Utility('Foo Bar!!!')
echo $str->isAlpha();
// false
boolCheck if the string is in an email format
$str = new Utility('foo@bar.com')
echo $str->isEmail();
// true
$str = new Utility('[@world](https://github.com/world) com')
echo $str->isEmail();
// false
boolCheck if the string is empty
$str = new Utility('')
echo $str->isEmpty();
// true
$str = new Utility('hello')
echo $str->isEmpty();
// false
$str = new Utility(' ')
echo $str->isEmpty();
// false
boolCheck if the string could be assumed to represent a false value ("false", "0", "no", "off", "")
$str = new Utility('false')
echo $str->isFalse();
// true
$str = new Utility('true')
echo $str->isFalse();
// false
boolCheck if the string is valid JSON
$str = new Utility('{ "foo":"bar", "hello":"world" }')
echo $str->isJson();
// true
$str = new Utility('"foo":"bar", "hello":"world"')
echo $str->isJson();
// false
boolCheck if the string contains no spaces or separators and only numeric characters
$str = new Utility('123')
echo $str->isNumeric();
// true
$str = new Utility('77 49')
echo $str->isNumeric();
// false
$str = new Utility('77.49')
echo $str->isNumeric();
// false
boolCheck if the string is not empty
$str = new Utility('hello')
echo $str->isNotEmpty();
// true
$str = new Utility('')
echo $str->isNotEmpty();
// false
boolCheck if the string could be assumed to represent a true value ("true", "1", "yes", "on", "ok")
$str = new Utility('true')
echo $str->isTrue();
// true
$str = new Utility('false')
echo $str->isTrue();
// false
UtilityGet the last x characters from the string
$str = new Utility('Hello World')
echo $str->last(5);
// World
echo $str->last(0);
// (empty string)
echo $str->last(50);
// Hello World
intGet the length of the string
$str = new Utility('hello world')
echo $str->length();
// 11
UtilityLimit the length of the string to a given value
$str = new Utility('Hello World')
echo $str->limit(5);
// Hello
boolCheck if the string matches a regular expression pattern
$str = new Utility('Hello=World')
echo $str->matches('/(.+)=(.+)/');
// true
echo $str->matches('/^[a-z\s]*$/i');
// false
UtilityMinimise the string removing all spaces and all unnecessary html attributes
$str = new Utility('foo <select><option>bar</option></select>')
echo $str->length();
// foo <select><option>foobar</select>
arrayFind the starting positions for all occurrences of a given value in the string
$str = new Utility('hello world. foo bar. food. foo bar. hello world.')
echo $str->occurrences('foo');
// [13, 22, 28]
UtilityRepeat a value on the left of the string, until it reaches a given length
$str = new Utility('foo')
echo $str->padLeft('!', 6);
// !!!foo
UtilityRepeat a value on the right of the string, until it reaches a given length
$str = new Utility('foo')
echo $str->padRight('!', 6);
// foo!!!
UtilityRepeat a value on both sides of the string, until it reaches a given length
$str = new Utility('foo')
echo $str->pad('!*', 7);
// !*foo!*
// !foo!!
echo $str->pad('.', 6);
// .foo..
UtilityPrepend a value to the string
$str = new Utility('World')
echo $str->prepend('Hello ');
// Hello World
UtilityStrip the string of any punctuation characters
$str = new Utility('Hello, World! It\'s a lovley day.')
echo $str->removePunctuation();
// Hello World Its a lovley day
UtilityStrip the string of any repeating characters
$str = new Utility('Foo Bar')
echo $str->removeRepeating(' ');
// Foo Bar
$str = new Utility('Hello World!!!!!!!!')
echo $str->removeRepeating('!');
// Hello World!
UtilityStrip the string of any space characters
$str = new Utility('Foo Bar. Hello World')
echo $str->removeSpace();
// FooBar.HelloWorld
UtilityRemove occurrences of a given value from the string
$str = new Utility('Foo Bar. Hello World')
echo $str->remove('o');
// F Bar Hell Wrld
UtilityRemove word from the start of the string
$str = new Utility('thethe quick brown fox')
echo $str->removeFromStart('the');
// the quick brown fox
UtilityRemove word from the end of the string
$str = new Utility('the quick brown foxfox')
echo $str->removeFromStart('fox');
// the quick brown fox
UtilityRepeat the string the amount of times specified
$str = new Utility('Foo Bar.')
echo $str->repeat(4);
// Foo Bar.Foo Bar.Foo Bar.Foo Bar.
UtilityReplace non alphanumeric values with a give value
Has an optional $strict parameter to preserve spaces or not, defaults to false
$str = new Utility('Foo Bar.')
echo $str->replaceNonAlphanumeric('');
// Foo Bar
echo $str->replaceNonAlphanumeric('', true);
// FooBar
UtilityReplace non alpha values with a give value
Has an optional $strict parameter to preserve spaces or not, defaults to false
$str = new Utility('Foo Bar. 123')
echo $str->replaceNonAlpha('');
// Foo Bar
echo $str->replaceNonAlpha('', true);
// FooBar
UtilityReplace non numeric values with a give value
Has an optional $strict parameter to preserve spaces or not, defaults to false
$str = new Utility('Foo Bar. 123')
echo $str->replaceNonNumeric('');
// 123
echo $str->replaceNonNumeric('', true);
// 123
UtilityReplace all occurrences of values from the string with another value
$str = new Utility('Foo Bar. 123')
echo $str->replace('123', '!!!');
// Foo Bar.
echo $str->replace(['Foo', 'Bar'], '');
// .123
UtilityReplace the first occurrence of a value in the string
$str = new Utility('foo bar foo')
echo $str->replaceFirst('foo', 'baz');
// baz bar foo
echo $str->replaceFirst('xyz', 'baz');
// foo bar foo
UtilityReplace the last occurrence of a value in the string
$str = new Utility('foo bar foo')
echo $str->replaceLast('foo', 'baz');
// foo bar baz
echo $str->replaceLast('xyz', 'baz');
// foo bar foo
UtilityReverse the string
$str = new Utility('foobar')
echo $str->reverse();
// raboof
UtilityCreate a slice of the string, starting at the index stated $start property for a length of the $end property
$str = new Utility('foobar')
echo $str->slice(0,3);
// foo
UtilityCreate a substring value of the string, starting at the index stated by $start property and up to and including index specified by $end property
If no $end value is provided, the rest of the string length is used
If a negative number is used for $end, it is calculated form the end of the string
$str = new Utility('foobar')
echo $str->slice(0,3);
// foo
$str = new Utility('foobar')
echo $str->slice(3);
// bar
$str = new Utility('foobar')
echo $str->slice(0,-3);
// foo
UtilityWrap a string with another string
$str = new Utility('Foo Bar')
echo $str->surround('!!!');
// !!!Foo Bar!!!
UtilitySwap multiple keywords in the string using a key/value map
$str = new Utility('foo bar')
echo $str->swap(['foo' => 'bar', 'bar' => 'foo']);
// bar foo
$str = new Utility('a-b-c')
echo $str->swap(['a' => '1', 'b' => '2', 'c' => '3']);
// 1-2-3
UtilityConvert the string to only contain alphanumeric values
$str = new Utility('Foo Bar 123!')
echo $str->toAlphanumeric();
// FooBar123
UtilityConvert the string to only contain alpha characters
$str = new Utility('Foo Bar 123!')
echo $str->toAlpha();
// FooBar
UtilityConvert the string to be in a camel case slug format
$str = new Utility('Foo Bar')
echo $str->toCamelCase();
// fooBar
$str = new Utility('Foo Bar!!! 123')
echo $str->toCamelCase();
// fooBar123
UtilityConvert the string to be in a kebab case slug format
$str = new Utility('Foo Bar')
echo $str->toKebabCase();
// foo-bar
$str = new Utility('Foo Bar!!! 123')
echo $str->toKebabCase();
// foo-bar-123
UtilityConvert the string to be all lowercase
$str = new Utility('HELLO WORLD')
echo $str->toLowercase();
// hello world
UtilityConvert the string to only contain numeric characters
$str = new Utility('Foo Bar 123!')
echo $str->toNumeric();
// 123
UtilityConvert the string to be in a pascal case slug format
$str = new Utility('Foo Bar')
echo $str->toPascalCase();
// FooBar
$str = new Utility('Foo Bar!!! 123')
echo $str->toPascalCase();
// FooBar123
UtilityConvert the string to be in a sentence case slug format
$str = new Utility('Foo Bar')
echo $str->toSentenceCase();
// Foo bar.
$str = new Utility('Foo Bar!!! 123')
echo $str->toSentenceCase();
// Foo Bar!!! 123.
UtilityConvert the string to be in a slug format
$str = new Utility('Foo Bar')
echo $str->toSentenceCase();
// foo-bar.
$str = new Utility('Foo Bar!!! 123')
echo $str->toSentenceCase();
// foo-bar-123.
UtilityConvert the string to be in a slug format but preserves utf8 characters
UtilityConvert the string to be in a snake case format
$str = new Utility('Foo Bar')
echo $str->toSnakeCase();
// foo_bar.
$str = new Utility('Foo Bar!!! 123')
echo $str->toSnakeCase();
// foo_bar_123.
UtilityConvert the string to be in a snake case format
$str = new Utility('foo bar')
echo $str->toStudlyCase();
// FooBar.
$str = new Utility('Foo Bar!!! 123')
echo $str->toStudlyCase();
// FooBar123.
UtilityConvert the string to be in a title case format
$str = new Utility('hello world! foo bar')
echo $str->toTitleCase();
// Hello World! Foo Bar.
UtilityConvert the string to be all uppercase
$str = new Utility('hello world')
echo $str->toUppercase();
// HELLO WORLD
UtilityTrim values from the left of the string
$str = new Utility('Hello World!')
echo $str->trimLeft('H');
// ello World!
$str = new Utility('Hello World!')
echo $str->trimLeft(['H', 'el']);
// o World!
UtilityTrim values from the right of the string
$str = new Utility('Hello World!')
echo $str->trimRight('!');
// Hello World
$str = new Utility('Hello World!')
echo $str->trimLeft(['Wor', 'ld!', ' ']);
// He
UtilityTrim values from the right of the string
$str = new Utility(' Hello World! ')
echo $str->trimRight(' ');
// Hello World!
$str = new Utility(' Hello World!')
echo $str->trimLeft(['!', ' ']);
// Hello World
UtilityGet the current value of the string
$str = new Utility('Foo Bar')
echo $str->value();
// Foo Bar
How can I help you explore Laravel packages today?