Code Monkey home page Code Monkey logo

twine's Introduction

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

afterappendbase64base64Decodebase64EncodebcryptbeforecamelCasecharacterschunkcontainscountcrc32cryptdecryptechoencodingencryptendsWithequalsexplodefirstformatfromhexhexEncodehexDecodeinsensitiveMatchinsertinisAlphabeticisAlphanumericisEmptyisLowercaseisNotEmptyisNumericisPrintableisPunctuationisUppercaseisWhitespacejoinkebabCaselastlengthlowercaselowercaseFirstlowercaseWordsmatchmatchAllmatchesmd5nthpadpadBothpadLeftpadRightpascalCaseprependrepeatreplacereversesha1sha256shufflesimilaritysnakeCasesplitstartsWithstripstudlyCasesubstringtotrimtrimLefttrimRighttruncateuppercaseuppercaseFirstuppercaseWordsurlwordswrapwrapHardwrapSoft


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.

twine's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

twine's Issues

Add Str::in() method

Determines if the string can be found within another string.

$string = new Twine\Str('pink');

$string->in('john pinkerton'); // Returns true

Question: Multi-Byte/Unicode/UTF-8 String Support

Chris,

I reviewed the README.md and docs, but didn't find any mentions (not to say they're not there) of multi-byte string handling or support. Maybe multi-byte string support is just one of the baked-in benefits of Twine?

When I reviewed some of the the code (master branch), it appeared in many, if not most, cases Twine utilizes PHP's multi-byte string functions available through its MBSTRING extension (https://www.php.net/manual/en/book.mbstring.php).

So, I wanted to ask. What's the goal or intent of the project with respect to multi-byte strings? For example, Does Twine intend to provide operations that are "multi-byte string" compatible/safe?

Thanks in advance for any response. Please forgive me if I overlooked this information in README.md or the docs. This seems like an appropriate means of asking the question, particularly since others may have similar questions and the issue and its correspondence would be there for posterity.

Regards,

Ben R

Allow prepend() and append() to accept unlimited prameters

Example

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

echo $first->append(' ', $last, ' ', 'jr'); // Returns 'john pinkerton jr'
$first = new Twine\Str('john');
$last = new Twine\Str('pinkerton');

echo $last->prepend('mr ', ' ', $first); // Returns 'mr john pinkerton'

join() method

API

Twine\Str::join($string, $glue = ' '); 

Example

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

$first->join($last); // Returns 'john pinkerton'
$min = new Twine\Str('1');
$max = new Twine\Str('100');

$min->join($max, '-'); // Returns '1-100'

Add explode()

What about adding explode()? I think it makes and isn't very hard :)

Please add encrypt and decrypt methods also

Hey, Awesome package. This is a feature request.

Would love to see these methods:

Normal: $string->encrypt($key) and $string->decrypt($key)

And optional Parameter for URL Safe: which is just urlencoded

URL Safe: $string->encrypt($key, Twine\Config\Encryption::SAFE) and just $string->decrypt($key)

Because it will be automatically urldecoded in the Request.

Alias: $string->encryptSafe($key) and just $string->decrypt($key)

Thank you.

Regular Expression Substrings

I would like to get part of the string by providing a regular expression that will be matched.

$string = new Twine\Str(
    'You can reach me on my cell at 123-456-7890 or at work 987-654-3210'
);

$string->match('/(?:\d{3}-?)\d{3}-?\d{4}/'); // Returns '123-456-7890'

This will return the first occurrence of a string that matches the given pattern.

Additionally we could have a matchAll() method that will return an array of matched patterns.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.