Skip to content
Michael Tils edited this page Mar 25, 2015 · 1 revision

The StringList is a class to simply build a string of separated parts. It extends the OrderedList so you can use all method of it.

Basic examples

use Collection\StringList;

$cssClasses = new StringList(['header','important','annotation']);

echo $cssClasses;
// prints 'header important annotation'
echo $cssClasses->append('inline');
// prints 'header important annotation inline'
echo $cssClasses->remove('important');
// prints 'header annotation inline'

You can also pass or assign a delimiter to split the "words"

use Collection\StringList;

$fruits = new StringList(['apple','orange','banana'], ', ');
echo $fruits;
// prints 'apple, orange, banana'

StringList also allows to set a prefix and a suffix

use Collection\StringList;

$fruits = new StringList(['id','name','email'], ', ');
$fruits->prefix = 'SELECT ';
$fruits->suffix = ' FROM users';
echo $fruits;
// prints 'SELECT id, name, email FROM users'

You could also use it as a string

use Collection\StringList;

$greeting = new StringList(['h','e','l','l','o'], '');
echo $greeting;
// prints 'hello'
echo $greeting->pop(0)->insert(0,'b');
// prints 'bello'

Build a StringList by a string

use Collection\StringList;

$greeting = StringList::fromString('id,name,email', ',');
// Result: StringList['id','name','email']

Clone this wiki locally