• Stars
    star
    496
  • Rank 88,480 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 7 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

String manipulation, leveled up!

Twine

String manipulation, leveled up! -- by, Chris Kankiewicz (@PHLAK)

Join the Community Become a Sponsor One-time Donation
Latest Stable Version Total Downloads GitHub branch checks state License


Introduction

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

Requirements

Install with Composer

composer require phlak/twine

Getting Started

First, import Twine:

use PHLAK\Twine;

Then instantiate a Twine object by newing up a Twine\Str object and passing your string as the first parameter.

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

You may also instantiate a Twine\Str object statically via the make() method.

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

Or use the global str() helper method. The method takes a string as the only parameter and returns a Twine\Str object.

$string = str('john pinkerton');

Once you have a concrete Twine\Str instance you may treat it like any other string. This includes echoing it or using any of PHP's built-in string functions against it.

echo $string; // Echos 'john pinkerton'

str_shuffle($string) // Returns something like 'enoipo ktnjhnr'

strlen($string); // Returns 14

The strength of Twine, however comes from its built-in methods.

$string->echo(); // Echos 'john pinkerton'
$string->shuffle(); // Returns something like 'enoipo ktnjhnr'
$string->length(); // Returns 14

// or some more interesting methods

$string->reverse(); // Returns 'notreknip nhoj'
$string->contains('pink'); // Returns true
$stting->replace('pink', 'purple'); // Returns 'john purpleton'
$string->snakeCase(); // Returns 'john_pinkerton'

At this point you're ready to start using Twine by calling any of its many built-in methods.

Available Methods

after β€’ append β€’ base64 β€’ base64Decode β€’ base64Encode β€’ bcrypt β€’ before β€’ camelCase β€’ characters β€’ chunk β€’ contains β€’ count β€’ crc32 β€’ crypt β€’ decrypt β€’ echo β€’ encoding β€’ encrypt β€’ endsWith β€’ equals β€’ explode β€’ first β€’ format β€’ from β€’ hex β€’ hexEncode β€’ hexDecode β€’ insensitiveMatch β€’ insert β€’ in β€’ isAlphabetic β€’ isAlphanumeric β€’ isEmpty β€’ isLowercase β€’ isNotEmpty β€’ isNumeric β€’ isPrintable β€’ isPunctuation β€’ isUppercase β€’ isWhitespace β€’ join β€’ kebabCase β€’ last β€’ length β€’ lowercase β€’ lowercaseFirst β€’ lowercaseWords β€’ match β€’ matchAll β€’ matches β€’ md5 β€’ nth β€’ pad β€’ padBoth β€’ padLeft β€’ padRight β€’ pascalCase β€’ prepend β€’ repeat β€’ replace β€’ reverse β€’ sha1 β€’ sha256 β€’ shuffle β€’ similarity β€’ snakeCase β€’ split β€’ startsWith β€’ strip β€’ studlyCase β€’ substring β€’ to β€’ trim β€’ trimLeft β€’ trimRight β€’ truncate β€’ uppercase β€’ uppercaseFirst β€’ uppercaseWords β€’ url β€’ words β€’ wrap β€’ wrapHard β€’ wrapSoft


Method Chaining

A Twine string can be manipulated fluently by chaining methods. Here are a few example chains:

Perform a substring comparison:

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

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

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);

Additional details available in the full documentation at https://twine.phlak.net.

MultiByte Strings

Twine aims for mltibyte string compatibility by relying on PHP's Multibyte String extension (mbstring) to perform string operations. For this reason, the mbstring extension is required. Multibyte strings include Unicode encodings such as UTF-8 and UCS-2.

Changelog

A list of changes can be found on the GitHub Releases page.

Troubleshooting

For general help and support join our GitHub Discussion or reach out on Twitter.

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is licensed under the MIT License.

More Repositories

1

SemVer

Semantic versioning helper library for PHP
PHP
169
star
2

Splat

Glob-like file and pattern matching utility.
PHP
106
star
3

docker-mumble

Docker image for Mumble server.
Dockerfile
87
star
4

Uses

The hardware and software I use on a regular basis.
60
star
5

Config

PHP library for simple configuration management
PHP
49
star
6

Soxy

Bash script for starting and stopping a SOCKS proxy tunnel over SSH in Linux.
Shell
26
star
7

StrGen

PHP library for simple, secure random string generation (e.g. - passwords / salts).
PHP
19
star
8

presentation

Presentation template using reveal.js + Tailwind CSS
HTML
11
star
9

Colorizer

PHP library for generating persistantly unique colors from a string.
PHP
11
star
10

docker-openvpn

Docker image for OpenVPN client/server.
Dockerfile
11
star
11

laravelcollective-html-form-snippets

Sublime Text 3 snippets for LaravelCollective/html form elements.
10
star
12

docker-minecraft

Docker image for (vanilla) Minecraft server.
Dockerfile
9
star
13

lifx-client

PHP client library for the LIFX API (v1)
PHP
8
star
14

vueColorClock

The color changing clock.
HTML
8
star
15

jColorClock

A color changing clock written in JavaScript.
JavaScript
6
star
16

Chronometer

Measure the passing of time.
PHP
6
star
17

Stash

Simple PHP caching library
PHP
6
star
18

CloudDrop

Store and retrieve files in cloud storage providers (i.e. Dropbox, Google Drive, Microsoft OneDrive, Amazon Cloud Drive, etc.)
PHP
5
star
19

sublime-text

My Sublime Text configuration.
Python
5
star
20

docker-speedtest

Docker image for the Speedtest CLI
Dockerfile
5
star
21

laravelcollective-html-form-snippets-atom

Atom snippets for LaravelCollective HTML form elements.
4
star
22

docker-syncthing

Docker image for Syncthing client/node.
Dockerfile
4
star
23

SemVer-CLI

Command line tool for managing semantic versioning within a project.
PHP
4
star
24

Resume.php

[DEPRECATED] My resume as a PHP class.
PHP
4
star
25

chriskankiewicz.com

My internet homepage.
PHP
3
star
26

docker-soxy

Docker image for SOCKS proxy client.
Dockerfile
3
star
27

github-backup

Shell + Python script for backing up GitHub repositories to local git bundles.
Shell
3
star
28

dont-fear-the-regex

Crash course presentation on regular expressions.
HTML
3
star
29

docker-flexget

Docker image for Flexget client.
Dockerfile
3
star
30

docker-hangoutsbot

Docker image for Hangoutsbot client.
Dockerfile
3
star
31

Cafelias

Randomly generated coffee slugs
PHP
2
star
32

docker-valheim

Docker image for Valheim dedicated server.
Dockerfile
2
star
33

restic-init

Initialization script for the restic backup program.
Shell
2
star
34

dotfiles

My custom configuration files for Linux.
Shell
2
star
35

docker-youtube-dl

Docker image for youtube-dl
Dockerfile
2
star
36

docker-transmission

Docker image for Transmission Daemon.
Dockerfile
2
star
37

string-manipulation-leveled-up

String manipulation, leveled up! Twine presentation slides.
JavaScript
2
star
38

CodingStandards

A pre-defined set of coding standards for PHP CS Fixer.
PHP
2
star
39

adguard-home-compose

A docker-compose configuration for Adguard Home
Shell
2
star
40

inner-circle-bot

The Inner Circle chat bot.
PHP
2
star
41

docker-iperf

Docker image for iperf server.
Dockerfile
2
star
42

Collection

Lightweight collection library
PHP
2
star
43

xhtml-transitional-template

A basic XHTML compliant web page template.
JavaScript
2
star
44

blog.chriskankiewicz.com

Pelican repository for http://blog.chriskankiewicz.com
HTML
1
star
45

dev-tools

An opiniated set of development packages and tools.
PHP
1
star
46

linux-machine-id

Get the unique machine ID on Linux.
Shell
1
star
47

webauth_redirect

Drupal module for redirecting anonymous users trying to access /user or /admin to an ASU webauth login page.
PHP
1
star
48

discord-bot

PHP
1
star
49

syncthing-compose

A docker-compose configuration for Syncthing.
Makefile
1
star
50

docker-crashplan

Docker image for CrashPlan client.
Shell
1
star
51

twine-docs

Twine Documentation
Blade
1
star
52

ubuntu-setup.sh

[DEPRECATED] Interactive script to automate Ubuntu setup after installation.
Shell
1
star
53

phlak.net

Official PHLAK.NET homepage.
JavaScript
1
star
54

link-shortener

Custom link shortener app
PHP
1
star
55

PyBlackjack

Personal project to learn some Python, allows you to play Blackjack.
Python
1
star
56

docker-ssh-tunnel

Docker image for SSH tunnel.
Dockerfile
1
star