Skip to content
This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
Anahkiasen edited this page Mar 19, 2013 · 15 revisions

Available methods

Note : A lot of those methods are directly taken from Laravel 3's String class (including the incredible mechanics behind the singular/plural methods). Credit goes to Taylor Otwell for them


Generate a string

String::accord(integer, many, one, [zero])

Creates a string from a number. You can provide a %d placeholder to insert the actual count into the final string.

String(15, '%d articles', '%d article', 'no articles') // Returns '15 articles'
String(1, '%d articles', '%d article', 'no articles') // Returns '1 article'
String(0, 'many articles', 'one article', 'no articles') // Returns 'no articles'

String::random(length)

Generates a random string

String::random(10) // Returns 'anFe48cfRc'

Informations about a string

String::endsWith(string, with)

Check if a string ends with another string

String::endsWith('foobar', 'bar') // Returns true

String::length(string)

Returns the length of a string

String::length('foobar') // Returns 6

String::startsWith(string, with)

Check if a string starts with another string

String::startsWith('foobar', 'foo') // Returns true

String::isIp(string)

Check if a string is an IP

String::isIp('192.168.1.1') // Returns true

String::isEmail(string)

Check if a string is an email

String::isEmail('[email protected]') // Returns true

String::isUrl(string)

Check if a string is an URL

String::isUrl('http://www.foo.com/') // Returns true

Fetch from a string

String::find(string, find, [caseSensitive], [absolute])

Find one or more needles in one or more haystacks. The third argument makes the search case sensitive, and the fourth argument if true ensures that all searchs need to match to return a true result

String::find('foobar', 'oob') // Returns true
String::find('foobar', array('foo', 'bar')) // Returns true
String::find(array('foo', 'bar'), 'foo') // Returns true

String::find('FOOBAR', 'foobar', false) // Returns false

String::find('foofoo', array('foo', 'bar'), false, true) // Returns false

String::slice(string, slice)

Slice a string with another string

String::slice('foobar', 'ba') // Returns array('foo', 'bar')

String::sliceFrom(string, slice)

Slice a string from a particular point

String::sliceFrom('foobar', 'ob') // Returns 'obar'

String::sliceTo(string, slice)

Slice a string up to a particular point

String::sliceTo('foobar', 'ob') // Returns 'fo'

Alter a string

String::append(string, with)

Append a string to another string

String::append('foo', 'bar') // Returns 'foobar'

String::explode(string, with, [limit])

Explodes a string by $with

String::explode("Joey doesn't share food", ' ') // Returns array('Joey', "doesn't", 'share', 'food')

String::limit(string, limit, [end])

Limits a string to X characters and append something

String::limit('This is somehow long', 10, '...') // Returns 'This is so...'

String::lower(string)

Converts a string to lowercase

String::lower('UNDerScoRe') // Returns 'underscore'
String::lower('ΆΧΙΣΤΗ') // Returns 'άχιστη'

String::plural(string)

Converts a string to plural

String::plural('child') // Returns 'children'

String::prepend(string, with)

Prepend a string with another string

String::prepend('foo', 'bar') // Returns 'barfoo'

String::remove(string, remove)

Remove one or more parts from a string

String::remove('foo', 'oo') // Return 'f'
String::remove('foo bar bis', array('bar', 'foo')) // Returns 'bis'

String::repeat(string, times)

Repeat a string X times

String::repeat('foo', 2) // Returns 'foofoo'

String::singular(string)

Converts a string to singular

String::singular('dogs') // Returns 'dog'

String::title(string)

Converts a string to title case

String::title('my little boat') // Returns 'My Little Boat'

String::toggle(string, first, second, [loose = false])

Toggles a string between two states. Contains a $loose flag on the last argument to allow the switching of a string that matches neither states

String::toggle('foo', 'foo', 'bar') // Returns 'bar'
String::toggle('bis', 'foo', 'bar', true) // Returns 'foo'

String::upper(string)

Converts a string to uppercase

String::upper('underscore') // Returns 'UNDERSCORE'
String::upper('άχιστη') // Returns 'ΆΧΙΣΤΗ'

String::words(string, limit, [end])

Limits a string to X words and append something

String::words('This is somehow long', 3, '...') // Returns 'This is somehow...'

Case switchers

String::toPascalCase(string, [limit])

Converts a string to PascalCase, or only the first $limit parts

String::toPascalCase('my_super_class') // Returns 'MySuperClass'
String::toPascalCase('my_super_class', 2) // Returns 'MySuper_class'

String::toSnakeCase(string, [limit])

Converts a string to snake_case, or only the first $limit parts

String::toSnakeCase('MySuperClass') // Returns 'my_super_class'
String::toSnakeCase('MySuperClass', 2) // Returns 'my_superClass'

String::toCamelCase(string, [limit])

Converts a string to CamelCase, or only the first $limit parts

String::toCamelCase('my_super_class') // Returns 'mySuperClass'
String::toCamelCase('my_super_class', 2) // Returns 'mySuper_class'