Download
:accept: Stringy
A PHP string manipulation library with multibyte support. Compatible with PHP 7+
100% compatible with the original "Stringy" library, but this fork is optimized
for performance and is using PHP 7+ features.
s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
Why?
In part due to a lack of multibyte support (including UTF-8) across many of
PHP's standard string functions. But also to offer an OO wrapper around the
mbstring module's multibyte-compatible functions. Stringy handles some quirks,
provides additional functionality, and hopefully makes strings a little easier
to work with!
// Standard library
strtoupper('fòôbà?'); // 'FòôBà?'
strlen('fòôbà?'); // 10
// mbstring
mb_strtoupper('fòôbà?'); // 'FÒÔBÀ?'
mb_strlen('fòôbà?'); // '6'
// Stringy
$stringy = Stringy\Stringy::create('fòôbà?');
$stringy->toUpperCase(); // 'FÒÔBÀ?'
$stringy->length(); // '6'
Alternative
If you like a more Functional Way to edit strings, then you can take a look at voku/portable-utf8, also "voku/Stringy" used the functions from the "Portable UTF-8"-Class but in a more Object Oriented Way.
// Portable UTF-8
use voku\helper\UTF8;
UTF8::strtoupper('fòôbà?'); // 'FÒÔBÀ?'
UTF8::strlen('fòôbà?'); // '6'
Installation via "composer require"
composer require voku/stringy
Installation via composer (manually)
If you're using Composer to manage dependencies, you can include the following
in your composer.json file:
"require": {
"voku/stringy": "~6.0"
}
Then, after running composer update or php composer.phar update , you can
load the class using Composer's autoloading:
require 'vendor/autoload.php';
Otherwise, you can simply require the file directly:
require_once 'path/to/Stringy/src/Stringy.php';
And in either case, I'd suggest using an alias.
use Stringy\Stringy as S;
OO and Chaining
The library offers OO method chaining, as seen below:
use Stringy\Stringy as S;
echo S::create('fòô bà?')->collapseWhitespace()->swapCase(); // 'FÒÔ BÀ?'
Stringy\Stringy has a __toString() method, which returns the current string
when the object is used in a string context, ie:
(string) S::create('foo') // 'foo'
Implemented Interfaces
Stringy\Stringy implements the IteratorAggregate interface, meaning that
foreach can be used with an instance of the class:
$stringy = S::create('fòôbà?');
foreach ($stringy as $char) {
echo $char;
}
// 'fòôbà?'
It implements the Countable interface, enabling the use of count() to
retrieve the number of characters in the string:
$stringy = S::create('fòô');
count($stringy); // 3
Furthermore, the ArrayAccess interface has been implemented. As a result,
isset() can be used to check if a character at a specific index exists. And
since Stringy\Stringy is immutable, any call to offsetSet or offsetUnset
will throw an exception. offsetGet has been implemented, however, and accepts
both positive and negative indexes. Invalid indexes result in an
OutOfBoundsException .
$stringy = S::create('bà?');
echo $stringy[2]; // '?'
echo $stringy[-2]; // 'à'
isset($stringy[-4]); // false
$stringy[3]; // OutOfBoundsException
$stringy[2] = 'a'; // Exception
PHP Class Call Creation
As of PHP 5.6+, use function is
available for importing functions. Stringy exposes a namespaced function,
Stringy\create , which emits the same behaviour as Stringy\Stringy::create() .
use function Stringy\create as s;
// Instead of: S::create('fòô bà?')
s('fòô bà?')->collapseWhitespace()->swapCase();
Class methods
create(mixed $str [, $encoding ])
Creates a Stringy object and assigns both str and encoding properties
the supplied values. $str is cast to a string prior to assignment, and if
$encoding is not specified, it defaults to mb_internal_encoding(). It
then returns the initialized object. Throws an InvalidArgumentException
if the first argument is an array or object without a __toString method.
$stringy = S::create('fòôbà?', 'UTF-8'); // 'fòôbà?'
If you need a collection of Stringy objects you can use the S::collection()
method.
$stringyCollection = \Stringy\collection(['fòôbà?', 'lall', 'öäü']);
Instance Methods
Stringy objects are immutable. All examples below make use of PHP 5.6
function importing, and PHP 5.4 short array syntax. They also assume the
encoding returned by mb_internal_encoding() is UTF-8. For further details,
see the documentation for the create method above.
%__functions_index__Stringy\Stringy__%
%__functions_list__Stringy\Stringy__%
Tests
From the project directory, tests can be ran using phpunit
Support
For support and donations please visit Github | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Twitter | Patreon.
For professional support please contact me.
Thanks
-
Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
-
Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
-
Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
-
Thanks to StyleCI for the simple but powerfull code style check.
-
Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!
License
Released under the MIT License - see LICENSE.txt for details.
|