Usage
Import & Instantiation
Once installed, you can start using Twine by importing the library.
use PHLAK\Twine;
// ...Next, instantiate a Twine Str object. There are multiple ways to accomplish this and no single method is inherently better than the others.
The default approach is to create a Twine\Str object directly.
$string = new Twine\Str('john pinkerton');IMPORTANT
When passing a non-string value to Twine\Str, it will be cast to a string internally.
You may also instantiate a Twine\Str object statically via the make() method.
$string = Twine\Str::make('john pinkerton');Twine also ships with a global str() helper that returns a Twine\Str object:
$string = str('john pinkerton');Using the Str object
Once you have a concrete Twine\Str instance, you may treat it like any other string in many situations.
echo $string; // Echos 'john pinkerton'
str_shuffle($string); // Returns something like 'enoipo ktnjhnr'
strlen($string); // Returns 14Keep in mind that a Twine\Str object is not a string primitive.
is_string($string); // Returns false
$string === 'john pinkerton'; // Returns falseAt this point, you're ready to start using Twine methods.
$string->substring(5, 4); // Returns 'pink'
$string->uppercaseWords(); // Returns 'John Pinkerton'
$string->words(); // Returns ['john', 'pinkerton']Method Chaining
A Twine string can be manipulated fluently by chaining methods.
Perform a substring comparison
$string = new Twine\Str('john pinkerton');
$string->substring(5, 4)->equals('pink'); // Returns trueTransform and manipulate a string
$string = Twine\Str::make('john pinkerton');
$string->prepend('mr. ')->uppercaseWords(); // Returns 'Mr. John Pinkerton'Encode a file in compliance with RFC 2045
$string = new Twine\Str(file_get_contents('garbage.bin'));
$string->base64()->wrap(76, "\r\n", Twine\Config\Wrap::HARD);Usage Tips
When using the static make() method or the str() helper, you can chain methods immediately in a single line.
Twine\Str::make('john pinkerton')->uppercaseWords();str('john pinkerton')->uppercaseWords();