A PHP string manipulation library with multibyte support. Compatible with PHP +5.3+ and HHVM. Refer to the 1.x branch +for older documentation.
- -diff --git a/index.html b/index.html index 8d217c5..26b91af 100644 --- a/index.html +++ b/index.html @@ -1,66 +1,76 @@ - - + +
- - +A PHP string manipulation library with multibyte support
+View the Project on GitHub danielstjules/Stringy
+A PHP string manipulation library with multibyte support. Compatible with PHP +5.3+ and HHVM. Refer to the 1.x branch +for older documentation.
- -A PHP string manipulation library with multibyte support. Offers both OO method -chaining and a procedural-style static wrapper. Tested and compatible with -PHP 5.3+ and HHVM. Inspired by underscore.string.js.
+s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
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
+s('fòôbàř')->toUpperCase(); // 'FÒÔBÀŘ'
+s('fòôbàř')->length(); // '6'
If you're using Composer to manage dependencies, you can include the following in your composer.json file:
-{
- "require": {
- "danielstjules/stringy": "~1.7"
- }
-}
-
"require": {
+ "danielstjules/stringy": "~2.0"
+}
Then, after running composer update
or php composer.phar update
, you can
load the class using Composer's autoloading:
require 'vendor/autoload.php';
-
require 'vendor/autoload.php';
Otherwise, you can simply require the file directly:
-require_once 'path/to/Stringy/src/Stringy.php';
-// or
-require_once 'path/to/Stringy/src/StaticStringy.php';
-
require_once 'path/to/Stringy/src/Stringy.php';
And in either case, I'd suggest using an alias.
-use Stringy\Stringy as S;
-// or
-use Stringy\StaticStringy as S;
-
use Stringy\Stringy as S;
Please note that Stringy relies on the mbstring
PHP module for its underlying
+multibyte support. This is a non-default, but very common module. For example,
+with debian and ubuntu, it's included in libapache2-mod-php5, php5-cli, and
+php5-fpm. For OSX users, it's a default for any version of PHP installed with
+homebrew. If compiling PHP from scratch, it can be included with the
+--enable-mbstring
flag.
The library offers both OO method chaining with Stringy\Stringy
, as well as
-procedural-style static method calls with Stringy\StaticStringy
. An example
-of the former is the following:
The library offers OO method chaining, as seen below:
-use Stringy\Stringy as S;
-echo S::create('Fòô Bàř', 'UTF-8')->collapseWhitespace()->swapCase(); // 'fÒÔ bÀŘ'
-
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'
Using the static wrapper, an alternative is the following:
- -use Stringy\StaticStringy as S;
-$string = S::collapseWhitespace('Fòô Bàř', 'UTF-8');
-echo S::swapCase($string, 'UTF-8'); // 'fÒÔ bÀŘ'
-
Stringy\Stringy
implements the IteratorAggregate
interface, meaning that
foreach
can be used with an instance of the class:
$stringy = S::create('Fòô Bàř', 'UTF-8');
-foreach ($stringy as $char) {
- echo $char;
-}
-// 'Fòô Bàř'
-
$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òô', 'UTF-8');
-count($stringy); // 3
-
$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
@@ -193,145 +217,33 @@ will throw an exception. offsetGet
has been implemented, however, a
both positive and negative indexes. Invalid indexes result in an
OutOfBoundsException
.
$stringy = S::create('Bàř', 'UTF-8');
-echo $stringy[2]; // 'ř'
-echo $stringy[-2]; // 'à'
-isset($stringy[-4]); // false
-
-$stringy[3]; // OutOfBoundsException
-$stringy[2] = 'a'; // Exception
-
$stringy = S::create('bàř');
+echo $stringy[2]; // 'ř'
+echo $stringy[-2]; // 'à'
+isset($stringy[-4]); // false
+
+$stringy[3]; // OutOfBoundsException
+$stringy[2] = 'a'; // Exception
In the list below, any static method other than S::create refers to a method in
-Stringy\StaticStringy
. For all others, they're found in Stringy\Stringy
.
-Furthermore, all methods that return a Stringy object or string do not modify
-the original. Stringy objects are immutable.
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()
.
+If running PHP 5.6, or another runtime that supports the use function
syntax,
+you can take advantage of an even simpler API as seen below:
Note: If $encoding
is not given, it defaults to mb_internal_encoding()
.
use function Stringy\create as s;
+
+// Instead of: S::create('fòô bàř')
+s('fòô bàř')->collapseWhitespace()->swapCase();
$stringy->at(int $index)
- -S::at(int $index [, string $encoding ])
- -Returns the character at $index, with indexes starting at 0.
- -S::create('fòô bàř', 'UTF-8')->at(6);
-S::at('fòô bàř', 6, 'UTF-8'); // 'ř'
-
$stringy->camelize();
- -S::camelize(string $str [, string $encoding ])
- -Returns a camelCase version of the string. Trims surrounding spaces, -capitalizes letters following digits, spaces, dashes and underscores, -and removes spaces, dashes, as well as underscores.
- -S::create('Camel-Case')->camelize();
-S::camelize('Camel-Case'); // 'camelCase'
-
$stringy->chars();
- -S::chars(string $str [, string $encoding ])
- -Returns an array consisting of the characters in the string.
- -S::create('Fòô Bàř', 'UTF-8')->chars();
-S::chars('Fòô Bàř', 'UTF-8'); // array(F', 'ò', 'ô', ' ', 'B', 'à', 'ř')
-
$stringy->collapseWhitespace()
- -S::collapseWhitespace(string $str [, string $encoding ])
- -Trims the string and replaces consecutive whitespace characters with a -single space. This includes tabs and newline characters, as well as -multibyte whitespace such as the thin space and ideographic space.
- -S::create(' Ο συγγραφέας ')->collapseWhitespace();
-S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγραφέας'
-
$stringy->contains(string $needle [, boolean $caseSensitive = true ])
- -S::contains(string $haystack, string $needle [, boolean $caseSensitive = true [, string $encoding ]])
- -Returns true if the string contains $needle, false otherwise. By default, -the comparison is case-sensitive, but can be made insensitive -by setting $caseSensitive to false.
- -S::create('Ο συγγραφέας είπε', 'UTF-8')->contains('συγγραφέας');
-S::contains('Ο συγγραφέας είπε', 'συγγραφέας', 'UTF-8'); // true
-
$stringy->containsAll(array $needles [, boolean $caseSensitive = true ])
- -S::containsAll(string $haystack, array $needles [, boolean $caseSensitive = true [, string $encoding ]])
- -Returns true if the string contains all $needles, false otherwise. By -default the comparison is case-sensitive, but can be made insensitive by -setting $caseSensitive to false.
- -S::create('Str contains foo and bar')->containsAll(array('foo', 'bar'));
-S::containsAll('Str contains foo and bar', array('foo', 'bar')); // true
-
$stringy->containsAny(array $needles [, boolean $caseSensitive = true ])
- -S::containsAny(string $haystack, array $needles [, boolean $caseSensitive = true [, string $encoding ]])
- -Returns true if the string contains any $needles, false otherwise. By -default the comparison is case-sensitive, but can be made insensitive by -setting $caseSensitive to false.
- -S::create('Str contains foo')->containsAny(array('foo', 'bar'));
-S::containsAny('Str contains foo', array('foo', 'bar')); // true
-
$stringy->countSubstr(string $substring [, boolean $caseSensitive = true ])
- -S::countSubstr(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
- -Returns the number of occurrences of $substring in the given string. -By default, the comparison is case-sensitive, but can be made insensitive -by setting $caseSensitive to false.
- -S::create('Ο συγγραφέας είπε', 'UTF-8')->countSubstr('α');
-S::countSubstr('Ο συγγραφέας είπε', 'α', 'UTF-8'); // 2
-
S::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 @@ -339,305 +251,333 @@ $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àř'
-
$stringy = S::create('fòôbàř', 'UTF-8'); // 'fòôbàř'
$stringy->dasherize();
+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, as well as the notes +on PHP 5.6 creation.
-S::dasherize(string $str [, string $encoding ])
+Returns a new string with $string appended.
+ +s('fòô')->append('bàř'); // 'fòôbàř'
Returns the character at $index, with indexes starting at 0.
+ +s('fòôbàř')->at(3); // 'b'
Returns the substring between $start and $end, if found, or an empty +string. An optional offset may be supplied from which to begin the +search for the start string.
+ +s('{foo} and {bar}')->between('{', '}'); // 'foo'
Returns a camelCase version of the string. Trims surrounding spaces, +capitalizes letters following digits, spaces, dashes and underscores, +and removes spaces, dashes, as well as underscores.
+ +s('Camel-Case')->camelize(); // 'camelCase'
Returns an array consisting of the characters in the string.
+ +s('fòôbàř')->chars(); // ['f', 'ò', 'ô', 'b', 'à', 'ř']
Trims the string and replaces consecutive whitespace characters with a +single space. This includes tabs and newline characters, as well as +multibyte whitespace such as the thin space and ideographic space.
+ +s(' Ο συγγραφέας ')->collapseWhitespace(); // 'Ο συγγραφέας'
Returns true if the string contains $needle, false otherwise. By default, +the comparison is case-sensitive, but can be made insensitive +by setting $caseSensitive to false.
+ +s('Ο συγγραφέας είπε')->contains('συγγραφέας'); // true
Returns true if the string contains all $needles, false otherwise. By +default the comparison is case-sensitive, but can be made insensitive by +setting $caseSensitive to false.
+ +s('foo & bar')->containsAll(['foo', 'bar']); // true
Returns true if the string contains any $needles, false otherwise. By +default the comparison is case-sensitive, but can be made insensitive by +setting $caseSensitive to false.
+ +s('str contains foo')->containsAny(['foo', 'bar']); // true
Returns the number of occurrences of $substring in the given string. +By default, the comparison is case-sensitive, but can be made insensitive +by setting $caseSensitive to false.
+ +s('Ο συγγραφέας είπε')->countSubstr('α'); // 2
Returns a lowercase and trimmed string separated by dashes. Dashes are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as underscores.
-S::create('TestDCase')->dasherize();
-S::dasherize('TestDCase'); // 'test-d-case'
-
s('fooBar')->dasherize(); // 'foo-bar'
$stringy->endsWith(string $substring [, boolean $caseSensitive = true ])
+Returns a lowercase and trimmed string separated by the given delimiter. +Delimiters are inserted before uppercase characters (with the exception +of the first character of the string), and in place of spaces, dashes, +and underscores. Alpha delimiters are not converted to lowercase.
-S::endsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
+s('fooBar')->delimit('::'); // 'foo::bar'
Returns true if the string ends with $substring, false otherwise. By default, the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
-S::create('FÒÔ bàřs', 'UTF-8')->endsWith('àřs', true);
-S::endsWith('FÒÔ bàřs', 'àřs', true, 'UTF-8'); // true
-
s('fòôbàř')->endsWith('bàř', true); // true
$stringy->ensureLeft(string $substring)
- -S::ensureLeft(string $substring [, string $encoding ])
+Ensures that the string begins with $substring. If it doesn't, it's prepended.
-S::create('foobar')->ensureLeft('http://');
-S::ensureLeft('foobar', 'http://'); // 'http://foobar'
-
s('foobar')->ensureLeft('http://'); // 'http://foobar'
$stringy->ensureRight(string $substring)
- -S::ensureRight(string $substring [, string $encoding ])
+Ensures that the string begins with $substring. If it doesn't, it's appended.
-S::create('foobar')->ensureRight('.com');
-S::ensureRight('foobar', '.com'); // 'foobar.com'
-
s('foobar')->ensureRight('.com'); // 'foobar.com'
$stringy->first(int $n)
- -S::first(int $n [, string $encoding ])
+Returns the first $n characters of the string.
-S::create('fòô bàř', 'UTF-8')->first(3);
-S::first('fòô bàř', 3, 'UTF-8'); // 'fòô'
-
s('fòôbàř')->first(3); // 'fòô'
$stringy->getEncoding()
+Returns the encoding used by the Stringy object.
-S::create('fòô bàř', 'UTF-8')->getEncoding(); // 'UTF-8'
-
s('fòôbàř', 'UTF-8')->getEncoding(); // 'UTF-8'
$stringy->humanize()
+Returns true if the string contains a lower case char, false otherwise.
-S::humanize(string $str [, string $encoding ])
+s('fòôbàř')->hasLowerCase(); // true
Returns true if the string contains an upper case char, false otherwise.
+ +s('fòôbàř')->hasUpperCase(); // false
Convert all HTML entities to their applicable characters. An alias of +html_entity_decode. For a list of flags, refer to +http://php.net/manual/en/function.html-entity-decode.php
+ +s('&')->htmlDecode(); // '&'
Convert all applicable characters to HTML entities. An alias of +htmlentities. Refer to http://php.net/manual/en/function.htmlentities.php +for a list of flags.
+ +s('&')->htmlEncode(); // '&'
Capitalizes the first word of the string, replaces underscores with spaces, and strips '_id'.
-S::create('author_id')->humanize();
-S::humanize('author_id'); // 'Author'
-
s('author_id')->humanize(); // 'Author'
$stringy->insert(int $index, string $substring)
+Returns the index of the first occurrence of $needle in the string, +and false if not found. Accepts an optional offset from which to begin +the search. A negative index searches from the end
-S::insert(string $str, int $index, string $substring [, string $encoding ])
+s('string')->indexOf('ing'); // 3
Returns the index of the last occurrence of $needle in the string, +and false if not found. Accepts an optional offset from which to begin +the search. Offsets may be negative to count from the last character +in the string.
+ +s('foobarfoo')->indexOfLast('foo'); // 10
Inserts $substring into the string at the $index provided.
-S::create('fòô bà', 'UTF-8')->insert('ř', 6);
-S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
-
s('fòôbř')->insert('à', 4); // 'fòôbàř'
$stringy->isAlpha()
- -S::isAlpha(string $str [, string $encoding ])
+Returns true if the string contains only alphabetic chars, false otherwise.
-S::create('丹尼爾', 'UTF-8')->isAlpha();
-S::isAlpha('丹尼爾', 'UTF-8'); // true
-
s('丹尼爾')->isAlpha(); // true
$stringy->isAlphanumeric()
- -S::isAlphanumeric(string $str [, string $encoding ])
+Returns true if the string contains only alphabetic and numeric chars, false otherwise.
-S::create('دانيال1', 'UTF-8')->isAlphanumeric();
-S::isAlphanumeric('دانيال1', 'UTF-8'); // true
-
s('دانيال1')->isAlphanumeric(); // true
$stringy->isBlank()
- -S::isBlank(string $str [, string $encoding ])
+Returns true if the string contains only whitespace chars, false otherwise.
-S::create("\n\t \v\f")->isBlank();
-S::isBlank("\n\t \v\f"); // true
-
s("\n\t \v\f")->isBlank(); // true
$stringy->isHexadecimal()
- -S::isHexadecimal(string $str [, string $encoding ])
+Returns true if the string contains only hexadecimal chars, false otherwise.
-S::create('A102F')->isHexadecimal();
-S::isHexadecimal('A102F'); // true
-
s('A102F')->isHexadecimal(); // true
$stringy->isJson()
- -S::isJson(string $str [, string $encoding ])
+Returns true if the string is JSON, false otherwise.
-S::create('{"foo":"bar"}')->isJson();
-S::isJson('{"foo":"bar"}'); // true
-
s('{"foo":"bar"}')->isJson(); // true
$stringy->isLowerCase()
- -S::isLowerCase(string $str [, string $encoding ])
+Returns true if the string contains only lower case chars, false otherwise.
-S::create('fòô bàř', 'UTF-8')->isLowerCase();
-S::isLowerCase('fòô bàř', 'UTF-8'); // true
-
s('fòôbàř')->isLowerCase(); // true
$stringy->isSerialized()
- -S::isSerialized(string $str [, string $encoding ])
+Returns true if the string is serialized, false otherwise.
-S::create('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8')->isSerialized();
-S::isSerialized('a:1:{s:3:"foo";s:3:"bar";}', 'UTF-8'); // true
-
s('a:1:{s:3:"foo";s:3:"bar";}')->isSerialized(); // true
$stringy->isUpperCase()
- -S::isUpperCase(string $str [, string $encoding ])
+Returns true if the string contains only upper case chars, false otherwise.
-S::create('FÒÔBÀŘ', 'UTF-8')->isUpperCase();
-S::isUpperCase('FÒÔBÀŘ', 'UTF-8'); // true
-
s('FÒÔBÀŘ')->isUpperCase(); // true
$stringy->last(int $n)
- -S::last(int $n [, string $encoding ])
+Returns the last $n characters of the string.
-S::create('fòô bàř', 'UTF-8')->last(3);
-S::last('fòô bàř', 3, 'UTF-8'); // 'bàř'
-
s('fòôbàř')->last(3); // 'bàř'
$stringy->length()
- -S::length(string $str [, string $encoding ])
+Returns the length of the string. An alias for PHP's mb_strlen() function.
-S::create('fòô bàř', 'UTF-8')->length();
-S::length('fòô bàř', 'UTF-8'); // 7
-
s('fòôbàř')->length(); // 6
$stringy->longestCommonPrefix(string $otherStr)
+Splits on newlines and carriage returns, returning an array of Stringy +objects corresponding to the lines in the string.
-S::longestCommonPrefix(string $str, string $otherStr [, $encoding ])
+s("fòô\r\nbàř\n")->lines(); // ['fòô', 'bàř', '']
Returns the longest common prefix between the string and $otherStr.
-S::create('fòô bar', 'UTF-8')->longestCommonPrefix('fòr bar');
-S::longestCommonPrefix('fòô bar', 'fòr bar', 'UTF-8'); // 'fò'
-
s('foobar')->longestCommonPrefix('foobaz'); // 'fooba'
$stringy->longestCommonSuffix(string $otherStr)
- -S::longestCommonSuffix(string $str, string $otherStr [, $encoding ])
+Returns the longest common suffix between the string and $otherStr.
-S::create('fòô bàř', 'UTF-8')->longestCommonSuffix('fòr bàř');
-S::longestCommonSuffix('fòô bàř', 'fòr bàř', 'UTF-8'); // ' bàř'
-
s('fòôbàř')->longestCommonSuffix('fòrbàř'); // 'bàř'
$stringy->longestCommonSubstring(string $otherStr)
- -S::longestCommonSubstring(string $str, string $otherStr [, $encoding ])
+Returns the longest common substring between the string and $otherStr. In the case of ties, it returns that which occurs first.
-S::create('foo bar')->longestCommonSubstring('boo far');
-S::longestCommonSubstring('foo bar', 'boo far'); // 'oo '
-
s('foobar')->longestCommonSubstring('boofar'); // 'oo'
$stringy->lowerCaseFirst();
- -S::lowerCaseFirst(string $str [, string $encoding ])
+Converts the first character of the supplied string to lower case.
-S::create('Σ test', 'UTF-8')->lowerCaseFirst();
-S::lowerCaseFirst('Σ test', 'UTF-8'); // 'σ test'
-
s('Σ foo')->lowerCaseFirst(); // 'σ foo'
$stringy->pad(int $length [, string $padStr = ' ' [, string $padType = 'right' ]])
- -S::pad(string $str , int $length [, string $padStr = ' ' [, string $padType = 'right' [, string $encoding ]]])
+Pads the string to a given length with $padStr. If length is less than or equal to the length of the string, no padding takes places. The default @@ -645,156 +585,106 @@ string used for padding is a space, and the default type (one of 'left', 'right', 'both') is 'right'. Throws an InvalidArgumentException if $padType isn't one of those 3 values.
-S::create('fòô bàř', 'UTF-8')->pad( 10, '¬ø', 'left');
-S::pad('fòô bàř', 10, '¬ø', 'left', 'UTF-8'); // '¬ø¬fòô bàř'
-
s('fòôbàř')->pad(9, '-/', 'left'); // '-/-fòôbàř'
$stringy->padBoth(int $length [, string $padStr = ' ' ])
- -S::padBoth(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
+Returns a new string of a given length such that both sides of the string string are padded. Alias for pad() with a $padType of 'both'.
-S::create('foo bar')->padBoth(9, ' ');
-S::padBoth('foo bar', 9, ' '); // ' foo bar '
-
s('foo bar')->padBoth(9, ' '); // ' foo bar '
$stringy->padLeft(int $length [, string $padStr = ' ' ])
- -S::padLeft(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
+Returns a new string of a given length such that the beginning of the string is padded. Alias for pad() with a $padType of 'left'.
-S::create($str, $encoding)->padLeft($length, $padStr);
-S::padLeft('foo bar', 9, ' '); // ' foo bar'
-
s('foo bar')->padLeft(9, ' '); // ' foo bar'
$stringy->padRight(int $length [, string $padStr = ' ' ])
- -S::padRight(string $str , int $length [, string $padStr = ' ' [, string $encoding ]])
+Returns a new string of a given length such that the end of the string is padded. Alias for pad() with a $padType of 'right'.
-S::create('foo bar')->padRight(10, '_*');
-S::padRight('foo bar', 10, '_*'); // 'foo bar_*_'
-
s('foo bar')->padRight(10, '_*'); // 'foo bar_*_'
$stringy->regexReplace(string $pattern, string $replacement [, string $options = 'msr'])
+Returns a new string starting with $string.
-S::regexReplace(string $str, string $pattern, string $replacement [, string $options = 'msr' [, string $encoding ]])
+s('bàř')->prepend('fòô'); // 'fòôbàř'
Replaces all occurrences of $pattern in $str by $replacement. An alias for mb_ereg_replace(). Note that the 'i' option with multibyte patterns -in mb_ereg_replace() requires PHP 5.4+. This is due to a lack of support -in the bundled version of Oniguruma in PHP 5.3.
+in mb_ereg_replace() requires PHP 5.6+ for correct results. This is due +to a lack of support in the bundled version of Oniguruma in PHP < 5.6, +and current versions of HHVM (3.8 and below). -S::create('fòô ', 'UTF-8')->regexReplace('f[òô]+\s', 'bàř', 'msr');
-S::regexReplace('fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'); // 'bàř'
-
s('fòô ')->regexReplace('f[òô]+\s', 'bàř', 'msr'); // 'bàř'
$stringy->removeLeft(string $substring)
- -S::removeLeft(string $str, string $substring [, string $encoding ])
+Returns a new string with the prefix $substring removed, if present.
-S::create('fòô bàř', 'UTF-8')->removeLeft('fòô ');
-S::removeLeft('fòô bàř', 'fòô ', 'UTF-8'); // 'bàř'
-
s('fòôbàř')->removeLeft('fòô'); // 'bàř'
$stringy->removeRight(string $substring)
- -S::removeRight(string $str, string $substring [, string $encoding ])
+Returns a new string with the suffix $substring removed, if present.
-S::create('fòô bàř', 'UTF-8')->removeRight(' bàř');
-S::removeRight('fòô bàř', ' bàř', 'UTF-8'); // 'fòô'
-
s('fòôbàř')->removeRight('bàř'); // 'fòô'
$stringy->replace(string $search, string $replacement)
+Returns a repeated string given a multiplier. An alias for str_repeat.
-S::replace(string $str, string $search, string $replacement [, string $encoding ])
+s('α')->repeat(3); // 'ααα'
Replaces all occurrences of $search in $str by $replacement.
-S::create('fòô bàř fòô bàř', 'UTF-8')->replace('fòô ', '');
-S::replace('fòô bàř fòô bàř', 'fòô ', '', 'UTF-8'); // 'bàř bàř'
-
s('fòô bàř fòô bàř')->replace('fòô ', ''); // 'bàř bàř'
$stringy->reverse()
- -S::reverse(string $str, [, string $encoding ])
+Returns a reversed string. A multibyte version of strrev().
-S::create('fòô bàř', 'UTF-8')->reverse();
-S::reverse('fòô bàř', 'UTF-8'); // 'řàb ôòf'
-
s('fòôbàř')->reverse(); // 'řàbôòf'
$stringy->safeTruncate(int $length, [, string $substring = '' ])
- -S::safeTruncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
+Truncates the string to a given length, while ensuring that it does not split words. If $substring is provided, and truncating occurs, the string is further truncated so that the substring may be appended without exceeding the desired length.
-S::create('What are your plans today?')->safeTruncate(22, '...');
-S::safeTruncate('What are your plans today?', 22, '...'); // 'What are your plans...'
-
s('What are your plans today?')->safeTruncate(22, '...');
+// 'What are your plans...'
$stringy->shuffle()
- -S::shuffle(string $str [, string $encoding ])
+A multibyte str_shuffle() function. It returns a string with its characters in random order.
-S::create('fòô bàř', 'UTF-8')->shuffle();
-S::shuffle('fòô bàř', 'UTF-8'); // 'àôřb òf'
-
s('fòôbàř')->shuffle(); // 'àôřbòf'
$stringy->slugify([ string $replacement = '-' ])
- -S::slugify(string $str [, string $replacement = '-' ])
+Converts the string into an URL slug. This includes replacing non-ASCII characters with their closest ASCII equivalents, removing remaining @@ -802,254 +692,204 @@ non-ASCII and non-alphanumeric characters, and replacing whitespace with $replacement. The replacement defaults to a single dash, and the string is also converted to lowercase.
-S::create('Using strings like fòô bàř')->slugify();
-S::slugify('Using strings like fòô bàř'); // 'using-strings-like-foo-bar'
-
s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar'
$stringy->startsWith(string $substring [, boolean $caseSensitive = true ])
- -S::startsWith(string $str, string $substring [, boolean $caseSensitive = true [, string $encoding ]])
+Returns true if the string begins with $substring, false otherwise. By default, the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.
-S::create('FÒÔ bàřs', 'UTF-8')->startsWith('fòô bàř', false);
-S::startsWith('FÒÔ bàřs', 'fòô bàř', false, 'UTF-8'); // true
-
s('FÒÔbàřbaz')->startsWith('fòôbàř', false); // true
$stringy->substr(int $start [, int $length ])
+Returns the substring beginning at $start, and up to, but not including +the index specified by $end. If $end is omitted, the function extracts +the remaining string. If $end is negative, it is computed from the end +of the string.
-S::substr(string $str, int $start [, int $length [, string $encoding ]])
+s('fòôbàř')->slice(3, -1); // 'bà'
Splits the string with the provided regular expression, returning an +array of Stringy objects. An optional integer $limit will truncate the +results.
+ +s('foo,bar,baz')->split(',', 2); // ['foo', 'bar']
Returns the substring beginning at $start with the specified $length. It differs from the mb_substr() function in that providing a $length of null will return the rest of the string, rather than an empty string.
-S::create('fòô bàř', 'UTF-8')->substr(2, 3);
-S::substr('fòô bàř', 2, 3, 'UTF-8'); // 'ô b'
-
s('fòôbàř')->substr(2, 3); // 'ôbà'
$stringy->surround(string $substring)
- -S::surround(string $str, string $substring)
+Surrounds a string with the given substring.
-S::create(' ͜ ')->surround('ʘ');
-S::surround(' ͜ ', 'ʘ'); // 'ʘ ͜ ʘ'
-
s(' ͜ ')->surround('ʘ'); // 'ʘ ͜ ʘ'
$stringy->swapCase();
- -S::swapCase(string $str [, string $encoding ])
+Returns a case swapped version of the string.
-S::create('Ντανιλ', 'UTF-8')->swapCase();
-S::swapCase('Ντανιλ', 'UTF-8'); // 'νΤΑΝΙΛ'
-
s('Ντανιλ')->swapCase(); // 'νΤΑΝΙΛ'
$stringy->tidy()
- -S::tidy(string $str)
+Returns a string with smart quotes, ellipsis characters, and dashes from Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.
-S::create('“I see…”')->tidy();
-S::tidy('“I see…”'); // '"I see..."'
-
s('“I see…”')->tidy(); // '"I see..."'
$stringy->titleize([ string $encoding ])
- -S::titleize(string $str [, array $ignore [, string $encoding ]])
+Returns a trimmed string with the first letter of each word capitalized. Ignores the case of other letters, preserving any acronyms. Also accepts an array, $ignore, allowing you to list words not to be capitalized.
-$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
-S::create('i like to watch DVDs at home', 'UTF-8')->titleize($ignore);
-S::titleize('i like to watch DVDs at home', $ignore, 'UTF-8');
-// 'I Like to Watch DVDs at Home'
-
$ignore = ['at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the'];
+s('If you optimize everything, you will always be unhappy.')->titleize($ignore);
+// 'I Like to Watch television'
$stringy->toAscii()
- -S::toAscii(string $str)
+Returns an ASCII version of the string. A set of non-ASCII characters are -replaced with their closest ASCII counterparts, and the rest are removed.
+replaced with their closest ASCII counterparts, and the rest are removed +unless instructed otherwise. -S::create('fòô bàř')->toAscii();
-S::toAscii('fòô bàř'); // 'foo bar'
-
s('fòôbàř')->toAscii(); // 'foobar'
$stringy->toLowerCase()
+Returns a boolean representation of the given logical string value. +For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0', +'off', and 'no' will return false. In all instances, case is ignored. +For other numeric strings, their sign will determine the return value. +In addition, blank strings consisting of only whitespace will return +false. For all other strings, the return value is a result of a +boolean cast.
-S::toLowerCase(string $str [, string $encoding ])
+s('OFF')->toBoolean(); // false
Converts all characters in the string to lowercase. An alias for PHP's mb_strtolower().
-S::create('FÒÔ BÀŘ', 'UTF-8')->toLowerCase();
-S::toLowerCase('FÒÔ BÀŘ', 'UTF-8'); // 'fòô bàř'
-
s('FÒÔBÀŘ')->toLowerCase(); // 'fòôbàř'
$stringy->toSpaces([ tabLength = 4 ])
- -S::toSpaces(string $str, [, int $tabLength = 4 ])
+Converts each tab in the string to some number of spaces, as defined by $tabLength. By default, each tab is converted to 4 consecutive spaces.
-S::create(' String speech = "Hi"')->toSpaces();
-S::toSpaces(' String speech = "Hi"'); // ' String speech = "Hi"'
-
s(' String speech = "Hi"')->toSpaces(); // ' String speech = "Hi"'
$stringy->toTabs([ tabLength = 4 ])
- -S::toTabs(string $str, [, int $tabLength = 4 ])
+Converts each occurrence of some consecutive number of spaces, as defined by $tabLength, to a tab. By default, each 4 consecutive spaces are converted to a tab.
-S::create(' fòô bàř')->toTabs();
-S::toTabs(' fòô bàř'); // ' fòô bàř'
-
s(' fòô bàř')->toTabs();
+// ' fòô bàř'
$stringy->toTitleCase()
- -S::toTitleCase(string $str [, string $encoding ])
+Converts the first character of each word in the string to uppercase.
-S::create('fòô bàř', 'UTF-8')->toTitleCase();
-S::toTitleCase('fòô bàř', 'UTF-8'); // 'Fòô Bàř'
-
s('fòô bàř')->toTitleCase(); // 'Fòô Bàř'
$stringy->toUpperCase()
- -S::toUpperCase(string $str [, string $encoding ])
+Converts all characters in the string to uppercase. An alias for PHP's mb_strtoupper().
-S::create('fòô bàř', 'UTF-8')->toUpperCase();
-S::toUpperCase('fòô bàř', 'UTF-8'); // 'FÒÔ BÀŘ'
-
s('fòôbàř')->toUpperCase(); // 'FÒÔBÀŘ'
$stringy->trim()
+Returns a string with whitespace removed from the start and end of the +string. Supports the removal of unicode whitespace. Accepts an optional +string of characters to strip instead of the defaults.
-S::trim(string $str)
+s(' fòôbàř ')->trim(); // 'fòôbàř'
Returns the trimmed string. An alias for PHP's trim() function.
+S::create('fòô bàř', 'UTF-8')->trim();
-S::trim(' fòô bàř '); // 'fòô bàř'
-
Returns a string with whitespace removed from the start of the string. +Supports the removal of unicode whitespace. Accepts an optional +string of characters to strip instead of the defaults.
-s(' fòôbàř ')->trimLeft(); // 'fòôbàř '
$stringy->truncate(int $length, [, string $substring = '' ])
+S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
+Returns a string with whitespace removed from the end of the string. +Supports the removal of unicode whitespace. Accepts an optional +string of characters to strip instead of the defaults.
+ +s(' fòôbàř ')->trimRight(); // ' fòôbàř'
Truncates the string to a given length. If $substring is provided, and truncating occurs, the string is further truncated so that the substring may be appended without exceeding the desired length.
-S::create('What are your plans today?')->truncate(19, '...');
-S::truncate('What are your plans today?', 19, '...'); // 'What are your pl...'
-
s('What are your plans today?')->truncate(19, '...'); // 'What are your pl...'
$stringy->underscored();
- -S::underscored(string $str [, string $encoding ])
+Returns a lowercase and trimmed string separated by underscores. Underscores are inserted before uppercase characters (with the exception of the first character of the string), and in place of spaces as well as dashes.
-S::create('TestUCase')->underscored();
-S::underscored('TestUCase'); // 'test_u_case'
-
s('TestUCase')->underscored(); // 'test_u_case'
$stringy->upperCamelize();
- -S::upperCamelize(string $str [, string $encoding ])
+Returns an UpperCamelCase version of the supplied string. It trims surrounding spaces, capitalizes letters following digits, spaces, dashes and underscores, and removes spaces, dashes, underscores.
-S::create('Upper Camel-Case')->upperCamelize();
-S::upperCamelize('Upper Camel-Case'); // 'UpperCamelCase'
-
s('Upper Camel-Case')->upperCamelize(); // 'UpperCamelCase'
$stringy->upperCaseFirst();
- -S::upperCaseFirst(string $str [, string $encoding ])
+Converts the first character of the supplied string to upper case.
-S::create('σ test', 'UTF-8')->upperCaseFirst();
-S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
-
s('σ foo')->upperCaseFirst(); // 'Σ foo'
The following is a list of libraries that extend Stringy:
@@ -1057,24 +897,30 @@ and underscores, and removes spaces, dashes, underscores.From the project directory, tests can be ran using phpunit
Released under the MIT License - see LICENSE.txt
for details.