Skip to content

Latest commit

 

History

History
991 lines (674 loc) · 16.6 KB

File metadata and controls

991 lines (674 loc) · 16.6 KB

Methods

after Utility

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 World

afterLast Utility

Get 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

append Utility

Append a value to the string

$str = new Utility('Hello World');

echo $str->append('!');
// Hello World!

at string

Get the character at a specific index

$str = new Utility('Hello World');

echo $str->at(6);
// W

before Utility

Get 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

beforeLast Utility

Get 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

beginsWith bool

BeginsWith defaults to case insensitive checks.

$str = new Utility('Hello World');

echo $str->beginsWith('hello');
// true

echo $str->beginsWith('hello', true);
// false

between Utility

Get 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

clean Utility

Clean 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>

contains bool

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);
// false

containsAll bool

Will 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

containsAny bool

Will 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

endsWith bool

Check 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

ensureBeingsWith Utility

Check 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

ensureEndsWith Utility

Check 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

equals bool

Compare the string with another

$str = new Utility('Foo Bar')

echo $str->equals('Foo Bar');
// true

echo $str->ensureEndsWith('foo bar');
// false

explode array

Explode 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']

format bool

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 

isAlphanumeric bool

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();
// false

isAlpha bool

Check if the string only contains alpha characters

$str = new Utility('FooBar')

echo $str->isAlpha();
// true

$str = new Utility('Foo Bar!!!')

echo $str->isAlpha();
// false

isEmail bool

Check 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();
// false

isEmpty bool

Check 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

isFalse bool

Check 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

isJson bool

Check 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

isNumeric bool

Check 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

isNotEmpty bool

Check if the string is not empty

$str = new Utility('hello')

echo $str->isNotEmpty();
// true

$str = new Utility('')

echo $str->isNotEmpty();
// false

isTrue bool

Check 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

last Utility

Get 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

length int

Get the length of the string

$str = new Utility('hello world')

echo $str->length();
// 11

limit Utility

Limit the length of the string to a given value

$str = new Utility('Hello World')

echo $str->limit(5);
// Hello

match bool

Check 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

minimise Utility

Minimise 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>

occurrences array

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]

padLeft Utility

Repeat a value on the left of the string, until it reaches a given length

$str = new Utility('foo')

echo $str->padLeft('!', 6);
// !!!foo

padRight Utility

Repeat a value on the right of the string, until it reaches a given length

$str = new Utility('foo')

echo $str->padRight('!', 6);
// foo!!!

pad Utility

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 Utility

Prepend a value to the string

$str = new Utility('World')

echo $str->prepend('Hello ');

// Hello World

removePunctuation Utility

Strip 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

removeRepeating Utility

Strip 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!

removeSpace Utility

Strip the string of any space characters

$str = new Utility('Foo        Bar. Hello World')

echo $str->removeSpace();
// FooBar.HelloWorld

remove Utility

Remove occurrences of a given value from the string

$str = new Utility('Foo Bar. Hello World')

echo $str->remove('o');
// F Bar Hell Wrld

removeFromStart Utility

Remove word from the start of the string

$str = new Utility('thethe quick brown fox')

echo $str->removeFromStart('the');
// the quick brown fox

removeFromEnd Utility

Remove word from the end of the string

$str = new Utility('the quick brown foxfox')

echo $str->removeFromStart('fox');
// the quick brown fox

repeat Utility

Repeat the string the amount of times specified

$str = new Utility('Foo Bar.')

echo $str->repeat(4);
// Foo Bar.Foo Bar.Foo Bar.Foo Bar.

replaceNonAlphanumeric Utility

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);
// FooBar

replaceNonAlpha Utility

Replace 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

replaceNonNumeric Utility

Replace 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

replace Utility

Replace 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

replaceFirst Utility

Replace 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

replaceLast Utility

Replace 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

reverse Utility

Reverse the string

$str = new Utility('foobar')

echo $str->reverse();
// raboof

slice Utility

Create 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

substring Utility

Create 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

surround Utility

Wrap a string with another string

$str = new Utility('Foo Bar')

echo $str->surround('!!!');
// !!!Foo Bar!!!

swap Utility

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-3

toAlphanumeric Utility

Convert the string to only contain alphanumeric values

$str = new Utility('Foo Bar 123!')

echo $str->toAlphanumeric();
// FooBar123

toAlpha Utility

Convert the string to only contain alpha characters

$str = new Utility('Foo Bar 123!')

echo $str->toAlpha();
// FooBar

toCamelCase Utility

Convert 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

toKebabCase Utility

Convert 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

toLowercase Utility

Convert the string to be all lowercase

$str = new Utility('HELLO WORLD')

echo $str->toLowercase();
// hello world

toNumeric Utility

Convert the string to only contain numeric characters

$str = new Utility('Foo Bar 123!')

echo $str->toNumeric();
// 123

toPascalCase Utility

Convert 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

toSentenceCase Utility

Convert 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.

toSlug Utility

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.

toSlugUtf8 Utility

Convert the string to be in a slug format but preserves utf8 characters

toSnakeCase Utility

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.

toStudlyCase Utility

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.

toTitleCase Utility

Convert the string to be in a title case format

$str = new Utility('hello world! foo bar')

echo $str->toTitleCase();
// Hello World! Foo Bar.

toUppercase Utility

Convert the string to be all uppercase

$str = new Utility('hello world')

echo $str->toUppercase();
// HELLO WORLD

trimLeft Utility

Trim 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!

trimRight Utility

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!', ' ']);
// He

trim Utility

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(['!', ' ']);
// Hello World

value Utility

Get the current value of the string

$str = new Utility('Foo Bar')

echo $str->value();
// Foo Bar