String manipulation,
leveled up!

Twitter
Chris Kankiewicz
Octocat

@PHLAK

Chris Kankiewicz

PHLAK

The Problem
with PHP's built-in string functions

Inconsistent & Unclear Naming

strstr sprintf
str_pad ucfirst
strtoupper strtok
strspn strpbrk

Inconsistent Parameters

str_pad ( string $input , mixed $pad_length , string $pad_string )

str_replace ( mixed $search , mixed $replace , mixed $subject )

wordwrap ( string $str , int $width )

explode ( string $delimiter , string $string )

Inside Out Operations

  1. Grab the contents of a binary file
  2. Encode the contents of that file to base64
  3. Wrap the base64 string at 80 characters
  4. Replace all newline characters with a <br> element
$contents = file_get_contents('garbage.bin');

str_replace("\n", '<br>', wordwrap(base64_encode($contents), 80));
$contents = file_get_contents('garbage.bin');
$base64 = base64_encode($contents);
$wrapped = wordwrap($base64, 80);

str_replace("\n", '<br>', $wrapped);

The Solution
or, how I learned to stop worrying and love strings again

Twine
https://twine.phlak.net

Twine is a string manipulation library
with an expressive fluent syntax.

By taking an object-oriented approach,
Twine makes string manipulation and
comparisons painless to read and write.

Using Twine
Let the fun begin!

composer install phlak/twine
use PHLAK\Twine;

$string = new Twine\Str('john pinkerton');

// or with the static make() method
$string = Twine\Str::make('john pinkerton');

// or with the helper function
$string = str('john pinkerton');

Treat it like a string

echo $string; // 'john pinkerton'

strlen($string); // 14
str_shuffle($string); // 'nnrjeh onpiotk'

$string == 'john pinkerton'; // true

$string[2]; // 'h'

Use the built-in methods

$string->substring(5, 4); // 'pink'

$string->equals('john pinkerton'); // true
$string->equals('jim ponkerton'); // false

$string->contains('pink'); // true
$string->contains('purple'); // false

$string->length(); // 14

Chain methods for fun and profit

$string->append(' jr')->uppercaseWords(); // 'John Pinkerton Jr'

$string->substring(5, 4)->equals('pink'); // true


Twine\Str::make('john pinkerton')->length(); // 14

str('john pinkerton')->shuffle(); // 'ohjine nokrtpn'

DEMO

Problem solved!

Consistent & Clear Naming

substring format
padLeft uppercaseFirst
lowercaseWords base64
camelCase wrapHard

Consistent Parameters

Twine\Str::pad ( int $length , string $padding , int $mode )

Twine\Str::replace ( string|array $search , string|array $replace )

Twine\Str::wrap ( int $width , string $break , bool $mode )

Outside-in Operations

$contents = file_get_contents('garbage.bin');

$string = new Twine\Str($contents);

$string->base64()->wrap(80)->replace("\n", '<br>');

Questions & Answers

What else would you like to see Twine do?

Audience Questions

$questions = [
    new Twine\Str('Shall we play a game?'),
    new Twine\Str("Aren't you a little short for a stormtrooper?"),
    new Twine\Str("WHAT'S IN THA BAHX?!!!")
];

Acknowledgments
I'd like to thank the academy...

Laravel News
https://laravel-news.com/twine-string-manipulation-leveled-up
azPHP
azPHP & Gordon
Twitter
Chris Kankiewicz
Octocat

@PHLAK

Chris Kankiewicz

PHLAK