-
Notifications
You must be signed in to change notification settings - Fork 88
String
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
- Informations about a string
- Fetch from a string
- Alter a string
- Case switchers
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'
Generates a random string
String::random(10) // Returns 'anFe48cfRc'
Check if a string ends with another string
String::endsWith('foobar', 'bar') // Returns true
Returns the length of a string
String::length('foobar') // Returns 6
Check if a string starts with another string
String::startsWith('foobar', 'foo') // Returns true
Check if a string is an IP
String::isIp('192.168.1.1') // Returns true
Check if a string is an email
String::isEmail('[email protected]') // Returns true
Check if a string is an URL
String::isUrl('http://www.foo.com/') // Returns true
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
Slice a string with another string
String::slice('foobar', 'ba') // Returns array('foo', 'bar')
Slice a string from a particular point
String::sliceFrom('foobar', 'ob') // Returns 'obar'
Slice a string up to a particular point
String::sliceTo('foobar', 'ob') // Returns 'fo'
Append a string to another string
String::append('foo', 'bar') // Returns 'foobar'
Explodes a string by $with
String::explode("Joey doesn't share food", ' ') // Returns array('Joey', "doesn't", 'share', 'food')
Limits a string to X characters and append something
String::limit('This is somehow long', 10, '...') // Returns 'This is so...'
Converts a string to lowercase
String::lower('UNDerScoRe') // Returns 'underscore'
String::lower('ΆΧΙΣΤΗ') // Returns 'άχιστη'
Converts a string to plural
String::plural('child') // Returns 'children'
Prepend a string with another string
String::prepend('foo', 'bar') // Returns 'barfoo'
Remove one or more parts from a string
String::remove('foo', 'oo') // Return 'f'
String::remove('foo bar bis', array('bar', 'foo')) // Returns 'bis'
Repeat a string X times
String::repeat('foo', 2) // Returns 'foofoo'
Converts a string to singular
String::singular('dogs') // Returns 'dog'
Converts a string to title case
String::title('my little boat') // Returns 'My Little Boat'
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'
Converts a string to uppercase
String::upper('underscore') // Returns 'UNDERSCORE'
String::upper('άχιστη') // Returns 'ΆΧΙΣΤΗ'
Limits a string to X words and append something
String::words('This is somehow long', 3, '...') // Returns 'This is somehow...'
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'
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'
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'