Get 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 WorldGet 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.bazAppend a value to the string
$str = new Utility('Hello World');
echo $str->append('!');
// Hello World!Get the character at a specific index
$str = new Utility('Hello World');
echo $str->at(6);
// WGet 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 WorldGet 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.bazBeginsWith defaults to case insensitive checks.
$str = new Utility('Hello World');
echo $str->beginsWith('hello');
// true
echo $str->beginsWith('hello', true);
// falseGet 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('=', '&');
// valueClean 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>Check 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);
// falseWill 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);
// falseWill 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);
// falseCheck 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']);
// falseCheck 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 BarCheck 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 WorldCompare the string with another
$str = new Utility('Foo Bar')
echo $str->equals('Foo Bar');
// true
echo $str->ensureEndsWith('foo bar');
// falseExplode 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']Replace 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 Check 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();
// falseCheck if the string only contains alpha characters
$str = new Utility('FooBar')
echo $str->isAlpha();
// true
$str = new Utility('Foo Bar!!!')
echo $str->isAlpha();
// falseCheck if the string is in an email format
$str = new Utility('foo@bar.com')
echo $str->isEmail();
// true
$str = new Utility('@world com')
echo $str->isEmail();
// falseCheck 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();
// falseCheck 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();
// falseCheck 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();
// falseCheck 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();
// falseCheck if the string is not empty
$str = new Utility('hello')
echo $str->isNotEmpty();
// true
$str = new Utility('')
echo $str->isNotEmpty();
// falseCheck 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();
// falseGet 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 WorldGet the length of the string
$str = new Utility('hello world')
echo $str->length();
// 11Limit the length of the string to a given value
$str = new Utility('Hello World')
echo $str->limit(5);
// HelloCheck if the string matches a regular expression pattern
$str = new Utility('Hello=World')
echo $str->matches('/(.+)=(.+)/');
// true
echo $str->matches('/^[a-z\s]*$/i');
// falseMinimise 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>Find 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]Repeat a value on the left of the string, until it reaches a given length
$str = new Utility('foo')
echo $str->padLeft('!', 6);
// !!!fooRepeat a value on the right of the string, until it reaches a given length
$str = new Utility('foo')
echo $str->padRight('!', 6);
// foo!!!Repeat 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..Prepend a value to the string
$str = new Utility('World')
echo $str->prepend('Hello ');
// Hello WorldStrip the string of any punctuation characters
$str = new Utility('Hello, World! It\'s a lovley day.')
echo $str->removePunctuation();
// Hello World Its a lovley dayStrip 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!Strip the string of any space characters
$str = new Utility('Foo Bar. Hello World')
echo $str->removeSpace();
// FooBar.HelloWorldRemove occurrences of a given value from the string
$str = new Utility('Foo Bar. Hello World')
echo $str->remove('o');
// F Bar Hell WrldRemove word from the start of the string
$str = new Utility('thethe quick brown fox')
echo $str->removeFromStart('the');
// the quick brown foxRemove word from the end of the string
$str = new Utility('the quick brown foxfox')
echo $str->removeFromStart('fox');
// the quick brown foxRepeat the string the amount of times specified
$str = new Utility('Foo Bar.')
echo $str->repeat(4);
// Foo Bar.Foo Bar.Foo Bar.Foo Bar.Replace 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);
// FooBarReplace 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);
// FooBarReplace 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);
// 123Replace 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'], '');
// .123Replace 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 fooReplace 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 fooReverse the string
$str = new Utility('foobar')
echo $str->reverse();
// raboofCreate 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);
// fooCreate 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);
// fooWrap a string with another string
$str = new Utility('Foo Bar')
echo $str->surround('!!!');
// !!!Foo Bar!!!Swap 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-3Convert the string to only contain alphanumeric values
$str = new Utility('Foo Bar 123!')
echo $str->toAlphanumeric();
// FooBar123Convert the string to only contain alpha characters
$str = new Utility('Foo Bar 123!')
echo $str->toAlpha();
// FooBarConvert 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();
// fooBar123Convert 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-123Convert the string to be all lowercase
$str = new Utility('HELLO WORLD')
echo $str->toLowercase();
// hello worldConvert the string to only contain numeric characters
$str = new Utility('Foo Bar 123!')
echo $str->toNumeric();
// 123Convert 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();
// FooBar123Convert 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.Convert 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.Convert the string to be in a slug format but preserves utf8 characters
Convert 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.Convert 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.Convert the string to be in a title case format
$str = new Utility('hello world! foo bar')
echo $str->toTitleCase();
// Hello World! Foo Bar.Convert the string to be all uppercase
$str = new Utility('hello world')
echo $str->toUppercase();
// HELLO WORLDTrim 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!Trim 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!', ' ']);
// HeTrim 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 WorldGet the current value of the string
$str = new Utility('Foo Bar')
echo $str->value();
// Foo Bar