A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

Overview

Stringy

A PHP string manipulation library with multibyte support. Compatible with PHP 5.4+, PHP 7+, and HHVM.

s('string')->toTitleCase()->ensureRight('y') == 'Stringy'

Refer to the 1.x branch or 2.x branch for older documentation.

Build Status Total Downloads License

append at between camelize
chars collapseWhitespace contains containsAll
containsAny countSubstr dasherize delimit
endsWith endsWithAny ensureLeft ensureRight
first getEncoding hasLowerCase hasUpperCase
htmlDecode htmlEncode humanize indexOf
indexOfLast insert isAlpha isAlphanumeric
isBase64 isBlank isHexadecimal isJson
isLowerCase isSerialized isUpperCase last
length lines longestCommonPrefix longestCommonSuffix
longestCommonSubstring lowerCaseFirst pad padBoth
padLeft padRight prepend regexReplace
removeLeft removeRight repeat replace
reverse safeTruncate shuffle slugify
slice split startsWith startsWithAny
stripWhitespace substr surround swapCase
tidy titleize toAscii toBoolean
toLowerCase toSpaces toTabs toTitleCase
toUpperCase trim trimLeft trimRight
truncate underscored upperCamelize upperCaseFirst

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
s('fòôbàř')->toUpperCase(); // 'FÒÔBÀŘ'
s('fòôbàř')->length();      // '6'

Installation

If you're using Composer to manage dependencies, you can include the following in your composer.json file:

"require": {
    "danielstjules/stringy": "~3.1.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;

Please note that Stringy relies on the mbstring module for its underlying multibyte support. If the module is not found, Stringy will use symfony/polyfill-mbstring. ex-mbstring 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.

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 5.6 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(). 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:

use function Stringy\create as s;

// Instead of: S::create('fòô     bàř')
s('fòô     bàř')->collapseWhitespace()->swapCase();

StaticStringy

All methods listed under "Instance methods" are available as part of a static wrapper. For StaticStringy methods, the optional encoding is expected to be the last argument. The return value is not cast, and may thus be of type Stringy, integer, boolean, etc.

use Stringy\StaticStringy as S;

// Translates to Stringy::create('fòôbàř')->slice(0, 3);
// Returns a Stringy object with the string "fòô"
S::slice('fòôbàř', 0, 3);

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àř'); // 'fòôbàř'

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, as well as the notes on PHP 5.6 creation.

append(string $string)

Returns a new string with $string appended.

s('fòô')->append('bàř'); // 'fòôbàř'
at(int $index)

Returns the character at $index, with indexes starting at 0.

s('fòôbàř')->at(3); // 'b'
between(string $start, string $end [, int $offset])

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'
camelize()

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'
chars()

Returns an array consisting of the characters in the string.

s('fòôbàř')->chars(); // ['f', 'ò', 'ô', 'b', 'à', 'ř']
collapseWhitespace()

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(); // 'Ο συγγραφέας'
contains(string $needle [, boolean $caseSensitive = true ])

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
containsAll(array $needles [, boolean $caseSensitive = 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
containsAny(array $needles [, boolean $caseSensitive = 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
countSubstr(string $substring [, boolean $caseSensitive = 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
dasherize()

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('fooBar')->dasherize(); // 'foo-bar'
delimit(int $delimiter)

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('fooBar')->delimit('::'); // 'foo::bar'
endsWith(string $substring [, boolean $caseSensitive = true ])

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('fòôbàř')->endsWith('bàř'); // true
endsWithAny(string[] $substrings [, boolean $caseSensitive = true ])

Returns true if the string ends with any of $substrings, false otherwise. By default, the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

s('fòôbàř')->endsWithAny(['bàř', 'baz']); // true
ensureLeft(string $substring)

Ensures that the string begins with $substring. If it doesn't, it's prepended.

s('foobar')->ensureLeft('http://'); // 'http://foobar'
ensureRight(string $substring)

Ensures that the string ends with $substring. If it doesn't, it's appended.

s('foobar')->ensureRight('.com'); // 'foobar.com'
first(int $n)

Returns the first $n characters of the string.

s('fòôbàř')->first(3); // 'fòô'
getEncoding()

Returns the encoding used by the Stringy object.

s('fòôbàř')->getEncoding(); // 'UTF-8'
hasLowerCase()

Returns true if the string contains a lower case char, false otherwise.

s('fòôbàř')->hasLowerCase(); // true
hasUpperCase()

Returns true if the string contains an upper case char, false otherwise.

s('fòôbàř')->hasUpperCase(); // false
htmlDecode()

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(); // '&'
htmlEncode()

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(); // '&'
humanize()

Capitalizes the first word of the string, replaces underscores with spaces, and strips '_id'.

s('author_id')->humanize(); // 'Author'
indexOf(string $needle [, $offset = 0 ]);

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('string')->indexOf('ing'); // 3
indexOfLast(string $needle [, $offset = 0 ]);

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
insert(int $index, string $substring)

Inserts $substring into the string at the $index provided.

s('fòôbř')->insert('à', 4); // 'fòôbàř'
isAlpha()

Returns true if the string contains only alphabetic chars, false otherwise.

s('丹尼爾')->isAlpha(); // true
isAlphanumeric()

Returns true if the string contains only alphabetic and numeric chars, false otherwise.

s('دانيال1')->isAlphanumeric(); // true
isBase64()

Returns true if the string is base64 encoded, false otherwise.

s('Zm9vYmFy')->isBase64(); // true
isBlank()

Returns true if the string contains only whitespace chars, false otherwise.

isBlank(); // true">
s("\n\t  \v\f")->isBlank(); // true
isHexadecimal()

Returns true if the string contains only hexadecimal chars, false otherwise.

s('A102F')->isHexadecimal(); // true
isJson()

Returns true if the string is JSON, false otherwise. Unlike json_decode in PHP 5.x, this method is consistent with PHP 7 and other JSON parsers, in that an empty string is not considered valid JSON.

isJson(); // true">
s('{"foo":"bar"}')->isJson(); // true
isLowerCase()

Returns true if the string contains only lower case chars, false otherwise.

s('fòôbàř')->isLowerCase(); // true
isSerialized()

Returns true if the string is serialized, false otherwise.

isSerialized(); // true">
s('a:1:{s:3:"foo";s:3:"bar";}')->isSerialized(); // true
isUpperCase()

Returns true if the string contains only upper case chars, false otherwise.

s('FÒÔBÀŘ')->isUpperCase(); // true
last(int $n)

Returns the last $n characters of the string.

s('fòôbàř')->last(3); // 'bàř'
length()

Returns the length of the string. An alias for PHP's mb_strlen() function.

s('fòôbàř')->length(); // 6
lines()

Splits on newlines and carriage returns, returning an array of Stringy objects corresponding to the lines in the string.

lines(); // ['fòô', 'bàř', '']">
s("fòô\r\nbàř\n")->lines(); // ['fòô', 'bàř', '']
longestCommonPrefix(string $otherStr)

Returns the longest common prefix between the string and $otherStr.

s('foobar')->longestCommonPrefix('foobaz'); // 'fooba'
longestCommonSuffix(string $otherStr)

Returns the longest common suffix between the string and $otherStr.

s('fòôbàř')->longestCommonSuffix('fòrbàř'); // 'bàř'
longestCommonSubstring(string $otherStr)

Returns the longest common substring between the string and $otherStr. In the case of ties, it returns that which occurs first.

s('foobar')->longestCommonSubstring('boofar'); // 'oo'
lowerCaseFirst()

Converts the first character of the supplied string to lower case.

s('Σ foo')->lowerCaseFirst(); // 'σ foo'
pad(int $length [, string $padStr = ' ' [, string $padType = 'right' ]])

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 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('fòôbàř')->pad(9, '-/', 'left'); // '-/-fòôbàř'
padBoth(int $length [, string $padStr = ' ' ])

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('foo bar')->padBoth(9, ' '); // ' foo bar '
padLeft(int $length [, string $padStr = ' ' ])

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('foo bar')->padLeft(9, ' '); // '  foo bar'
padRight(int $length [, string $padStr = ' ' ])

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('foo bar')->padRight(10, '_*'); // 'foo bar_*_'
prepend(string $string)

Returns a new string starting with $string.

s('bàř')->prepend('fòô'); // 'fòôbàř'
regexReplace(string $pattern, string $replacement [, string $options = 'msr'])

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.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('fòô ')->regexReplace('f[òô]+\s', 'bàř'); // 'bàř'
s('fò')->regexReplace('(ò)', '\\1ô'); // 'fòô'
removeLeft(string $substring)

Returns a new string with the prefix $substring removed, if present.

s('fòôbàř')->removeLeft('fòô'); // 'bàř'
removeRight(string $substring)

Returns a new string with the suffix $substring removed, if present.

s('fòôbàř')->removeRight('bàř'); // 'fòô'
repeat(int $multiplier)

Returns a repeated string given a multiplier. An alias for str_repeat.

s('α')->repeat(3); // 'ααα'
replace(string $search, string $replacement)

Replaces all occurrences of $search in $str by $replacement.

s('fòô bàř fòô bàř')->replace('fòô ', ''); // 'bàř bàř'
reverse()

Returns a reversed string. A multibyte version of strrev().

s('fòôbàř')->reverse(); // 'řàbôòf'
safeTruncate(int $length [, string $substring = '' ])

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('What are your plans today?')->safeTruncate(22, '...');
// 'What are your plans...'
shuffle()

A multibyte str_shuffle() function. It returns a string with its characters in random order.

s('fòôbàř')->shuffle(); // 'àôřbòf'
slugify([, string $replacement = '-' [, string $language = 'en']])

Converts the string into an URL slug. This includes replacing non-ASCII characters with their closest ASCII equivalents, removing remaining 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. The language of the source string can also be supplied for language-specific transliteration.

s('Using strings like fòô bàř')->slugify(); // 'using-strings-like-foo-bar'
slice(int $start [, int $end ])

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('fòôbàř')->slice(3, -1); // 'bà'
split(string $pattern [, int $limit ])

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']
startsWith(string $substring [, boolean $caseSensitive = true ])

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('FÒÔbàřbaz')->startsWith('fòôbàř', false); // true
startsWithAny(string[] $substrings [, boolean $caseSensitive = true ])

Returns true if the string begins with any of $substrings, false otherwise. By default the comparison is case-sensitive, but can be made insensitive by setting $caseSensitive to false.

s('FÒÔbàřbaz')->startsWithAny(['fòô', 'bàř'], false); // true
stripWhitespace()

Strip all whitespace characters. This includes tabs and newline characters, as well as multibyte whitespace such as the thin space and ideographic space.

s('   Ο     συγγραφέας  ')->stripWhitespace(); // 'Οσυγγραφέας'
substr(int $start [, int $length ])

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('fòôbàř')->substr(2, 3); // 'ôbà'
surround(string $substring)

Surrounds a string with the given substring.

s(' ͜ ')->surround('ʘ'); // 'ʘ ͜ ʘ'
swapCase()

Returns a case swapped version of the string.

s('Ντανιλ')->swapCase(); // 'νΤΑΝΙΛ'
tidy()

Returns a string with smart quotes, ellipsis characters, and dashes from Windows-1252 (commonly used in Word documents) replaced by their ASCII equivalents.

s('“I see…”')->tidy(); // '"I see..."'
titleize([, array $ignore])

Returns a trimmed string with the first letter of each word capitalized. Also accepts an array, $ignore, allowing you to list words not to be capitalized.

$ignore = ['at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the'];
s('i like to watch television')->titleize($ignore);
// 'I Like to Watch Television'
toAscii([, string $language = 'en' [, bool $removeUnsupported = true ]])

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 by default. The language or locale of the source string can be supplied for language-specific transliteration in any of the following formats: en, en_GB, or en-GB. For example, passing "de" results in "äöü" mapping to "aeoeue" rather than "aou" as in other languages.

s('fòôbàř')->toAscii(); // 'foobar'
s('äöü')->toAscii(); // 'aou'
s('äöü')->toAscii('de'); // 'aeoeue'
toBoolean()

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('OFF')->toBoolean(); // false
toLowerCase()

Converts all characters in the string to lowercase. An alias for PHP's mb_strtolower().

s('FÒÔBÀŘ')->toLowerCase(); // 'fòôbàř'
toSpaces([, 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.

toSpaces(); // ' String speech = "Hi"'">
s(' String speech = "Hi"')->toSpaces(); // '    String speech = "Hi"'
toTabs([, 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('    fòô    bàř')->toTabs();
// '   fòô bàř'
toTitleCase()

Converts the first character of each word in the string to uppercase.

s('fòô bàř')->toTitleCase(); // 'Fòô Bàř'
toUpperCase()

Converts all characters in the string to uppercase. An alias for PHP's mb_strtoupper().

s('fòôbàř')->toUpperCase(); // 'FÒÔBÀŘ'
trim([, string $chars])

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('  fòôbàř  ')->trim(); // 'fòôbàř'
trimLeft([, string $chars])

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àř  '
trimRight([, string $chars])

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àř'
truncate(int $length [, string $substring = '' ])

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('What are your plans today?')->truncate(19, '...'); // 'What are your pl...'
underscored()

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('TestUCase')->underscored(); // 'test_u_case'
upperCamelize()

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('Upper Camel-Case')->upperCamelize(); // 'UpperCamelCase'
upperCaseFirst()

Converts the first character of the supplied string to upper case.

s('σ foo')->upperCaseFirst(); // 'Σ foo'

Extensions

The following is a list of libraries that extend Stringy:

Tests

From the project directory, tests can be ran using phpunit

License

Released under the MIT License - see LICENSE.txt for details.

Comments
  • Ensuring, that __toString would not cause fatal errors

    Ensuring, that __toString would not cause fatal errors

    __toString method in PHP can cause: Catchable fatals http://sandbox.onlinephpfunctions.com/code/f9fc435ff703a2310e25ded726a661389ab3821b

    Fatals with interesting description http://sandbox.onlinephpfunctions.com/code/1a2324bc459e8a5066c5e9b07bc53b3b69964051

    To avoid first one you can use implicit cast function __toString(){ return (string) $str; } But if you want to avoid both, you must cast to string in setter method (i.e. in constructor) and make Stringy->str private

    opened by gotterdemarung 21
  • Add link to PHP array manipulation lib in OOP way

    Add link to PHP array manipulation lib in OOP way

    I was inspired by Stringy and made a similar library in order to work with native PHP arrays in OOP way.

    Could I add a partner link to my library in your README?

    opened by bocharsky-bw 20
  • Is it supposed to work on configurations without mbstring module installed?

    Is it supposed to work on configurations without mbstring module installed?

    I saw the mention in description that it uses the polyfill when the module is not installed, however seems that it's not sufficient?

    Fatal error: Uncaught Error: Call to undefined function mb_regex_encoding()

    The product that uses Stringy is supposed to be distributed to users that may be unable to install the mbstring module.

    Tested on php PHP 7.0.4-6+deb.sury.org~trusty+3

    opened by chetzof 17
  • [Proposal] Separate toAscii()'s charsArray to allow customization

    [Proposal] Separate toAscii()'s charsArray to allow customization

    Again, this is just a proposal. Fairly related to #68.

    I used a method instead of a property to not clutter the top of the file. Very simple to extend:

    protected function charsArray()
    {
        $chars = parent::charsArray();
        // do stuff
    
        return $chars;
    }
    

    Or, using some cache for optimal performances:

    protected function charsArray()
    {
        static $chars;
    
        if (!isset($chars)) {
            $chars = parent::charsArray();
            // do stuff
        }
    
        return $chars;
    }
    
    opened by vlakoff 17
  • Localization problem

    Localization problem

    Hi,

    I'am using laravel 5.2.5 and this package using small part codes of Stringy. I have problem with some Turkish character (ö, Ö, ü, U) converting to slug.

    For example in Turkish Language:

    'Ö' => 'O', 'ö' => 'o',

    in German or etc

    'Ö' => 'OE', 'ö' => 'oe',

    It is really big problem. (for information: https://en.wikipedia.org/wiki/%C3%96)

    Converting process must be by localization.

    Here laravel issue: https://github.com/laravel/framework/issues/11619

    opened by ozgurkaragoz 15
  • Methods to work with SubStrings, find Offsets and Evaluate String Contents

    Methods to work with SubStrings, find Offsets and Evaluate String Contents

    Added multiple methods to work with substrings, find offsets and evaluate string contents. fixes #86

    /**
    * Returns the offset/index of the last occurance of $substr in the string.
    * In case $substr is not a substring of the string, returns false.
    *
    * @param string $substr substring
    * @param int $offset
    * @return int|bool
    */
    public function offsetOfSubstr($substr, $offset = 0);
    
    /**
     * Returns the offset/index of the last occurance of $substr in the string.
     * In case $substr is not a substring of the string, returns false.
     *
     * @param string $substr substring
     * @param int $offset
     * @return int|bool
     */
    public function offsetOfLastSubstr($substr, $offset = 0);
    
    /**
    * Returns true if the string is empty (doesn't contain any char),
    * false otherwise.
    *
    * @return bool Whether or not $str is empty (no chars)
    */
    public function isEmpty();
    
    /**
     * Returns true if the string contains an integer value, false
     * otherwise.
     *
     * @return bool Whether or not $str contains an integer value
     */
    public static function containsInt();
    
    /**
     * Returns true if the string contains an float value, false
     * otherwise.
     *
     * @return bool Whether or not $str contains an float value
     */
    public static function constainsFloat();
    
     /**
     * Gets the substring after the first occurrence of a separator.
     * If no match is found returns false.
     * 
     * @param string $separator
     * @return string|bool
     */
    public function substringAfterFirst($separator);
    
    /**
     * Gets the substring after the last occurrence of a separator.
     * If no match is found returns false.
     * 
     * @param string $separator
     * @return string|bool
     */
    public function substringAfterLast($separator);
    
    /**
     * Gets the substring before the first occurrence of a separator.
     * If no match is found returns false.
     * 
     * @param string $separator
     * @return string|bool
     */
    public function substringBeforeFirst($separator);
    
    /**
     * Gets the substring before the last occurrence of a separator.
     * If no match is found returns false.
     * 
     * @param string $separator
     * @return string|bool
     */
    public function substringBeforeLast($separator);
    
    opened by TCB13 14
  • PHP bug with mb_strtoupper and mb_strtolower

    PHP bug with mb_strtoupper and mb_strtolower

    Just got bitten by this bug: https://bugs.php.net/bug.php?id=47742

    Stringy's toLowerCase and toUpperCase use them.

    This seems to be a workaround that works for me:

    mb_convert_case($string, MB_CASE_UPPER, 'UTF-8');
    mb_convert_case($string, MB_CASE_LOWER, 'UTF-8');
    

    Interestingly enough, you're already using mb_convert_case in the toTitleCase() method.

    opened by angrybrad 10
  • Cannot redeclare Stringy\create()

    Cannot redeclare Stringy\create()

    Fatal error: Cannot redeclare Stringy\create() (previously declared in /Users/crynobone/.composer/vendor/danielstjules/stringy/src/Create.php:14) in /Users/crynobone/Projects/orchestra/testbench/vendor/danielstjules/stringy/src/Create.php on line 17
    

    Since this package is now used by illuminate/support which commonly installed per project and globally the function need to be wrapped inside function_exists() to work properly.

    See composer/composer#3003 and reactphp/promise#23 for reference.

    opened by crynobone 10
  • Rewrote Stringy::pad to be DRY.

    Rewrote Stringy::pad to be DRY.

    Changed Stringy::pad accessibility to private. Added PHPUnit to Composer's require-dev list. Fixed "Class 'CommonTest' not found" error when running specific tests in PHPUnit by including tests directory in Composer's autoloader.

    opened by Bilge 10
  • type cast in removeLeft && removeRight

    type cast in removeLeft && removeRight

    I don't even know if it is unexpected behavior or not, but when I write

    $string->removeLeft($string->first(1))->removeRight($string->last(1))
    

    I expect that first and last characters of string are removed.

    Also I want to add more shortcuts and some functional to Stringy, what do you think about it?

    • removeFirst
    • removeLast
    • extractDigits
    • extractLetters
    • ...

    It's super easy to implement, and they can save some time for users of package.

    @danielstjules Do Stringy needs this functional, what do you think ? Thank you for your time!

    opened by usernam3 9
  • titleize() works unexpectedly on strings with all caps

    titleize() works unexpectedly on strings with all caps

    I was expecting the titilize method to return "Title Case" for the string "TITLE CASE". Instead, it stayed all caps. I had to call toLower, then titleize to get the desired result.

    opened by stevenmusumeche 8
  • PHP 8.1 compatibility

    PHP 8.1 compatibility

    Deprecated: Return type of Stringy\Stringy::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /.../vendor/danielstjules/stringy/src/Stringy.php on line 893

    The solution could be very similar to this: https://github.com/cebe/php-openapi/pull/143/files


    Return type of Stringy\Stringy::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice.

    opened by Sweetchuck 7
  • [PHP 8.1] Add #[\ReturnTypeWillChange]

    [PHP 8.1] Add #[\ReturnTypeWillChange]

    without this, it can cause error :

    PHP Deprecated:  Return type of Stringy\Stringy::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 270
    Deprecated: Return type of Stringy\Stringy::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 270
    PHP Deprecated:  Return type of Stringy\Stringy::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 453
    Deprecated: Return type of Stringy\Stringy::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 453
    PHP Deprecated:  Return type of Stringy\Stringy::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 850
    Deprecated: Return type of Stringy\Stringy::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 850
    PHP Deprecated:  Return type of Stringy\Stringy::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 873
    Deprecated: Return type of Stringy\Stringy::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 873
    PHP Deprecated:  Return type of Stringy\Stringy::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 893
    Deprecated: Return type of Stringy\Stringy::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 893
    PHP Deprecated:  Return type of Stringy\Stringy::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 906
    Deprecated: Return type of Stringy\Stringy::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/runner/work/rector-src/rector-src/vendor/danielstjules/stringy/src/Stringy.php on line 906
    
    opened by samsonasik 0
  • Line break normalization

    Line break normalization

    I suggest add feature for normalization line breaks. RegEx have modifier \R that matches any Unicode newline sequence. Equivalent to (?>\r\n|\n|\x0b|\f|\r|\x85). And the sources texts are sometimes very unpredictable with line breaks.

    May be conside normalization method something like

    	/**
    	 * Normalizes all line endings in this string by using a single unified newline sequence (which may be specified manually)
    	 *
    	 * @param string $text source text for normalize
    	 * @param string|null $newlineSequence the target newline sequence to use (optional)
    	 * @return string normalized text
    	 */
    	public function normalizeLineEndings($text, $newlineSequence = null) {
    		if ($newlineSequence === null) {
    			$newlineSequence = "\n";
    		}
    
    		return \preg_replace('/\R/u', $newlineSequence, $text);
    	}
    

    (С) Borrowed this handy solution from the library https://github.com/delight-im/PHP-Str

    opened by quantum3k 0
Releases(3.1.0)
  • 3.1.0(Jun 12, 2017)

  • 3.0.1(Apr 12, 2017)

  • 3.0.0(Mar 9, 2017)

    • Breaking change: added $language parameter to toAscii, before $removeUnsupported
    • Breaking change: dropped PHP 5.3 support
    • Breaking change: any StaticStringy methods that previously returned instances of Stringy now return strings
    Source code(tar.gz)
    Source code(zip)
  • 2.4.0(Mar 2, 2017)

    • Add startsWithAny
    • Add endsWithAny
    • Add stripWhitespace
    • Fix error handling for unsupported encodings
    • Change private methods to protected for extending class
    • Fix safeTruncate for strings without spaces
    • Additional char support in toAscii, e.g. full width chars and wide non-breaking space
    Source code(tar.gz)
    Source code(zip)
  • 2.3.2(May 2, 2016)

  • 2.3.1(Mar 21, 2016)

  • 2.3.0(Mar 20, 2016)

  • 2.2.0(Dec 21, 2015)

    • isJSON now returns false for empty strings
    • Update for German umlaut transformation
    • Use reflection to generate method list for StaticStringy
    • Added isBase64 method
    • Improved toAscii char coverage
    Source code(tar.gz)
    Source code(zip)
  • 2.1.0(Sep 3, 2015)

  • 2.0.0(Jul 29, 2015)

    • Removed StaticStringy class
    • Added append, prepend, toBoolean, repeat, between, slice, split, and lines
    • camelize/upperCamelize now strip leading dashes and underscores
    • titleize converts to lowercase, thus no longer preserving acronyms
    Source code(tar.gz)
    Source code(zip)
  • 1.10.0(Jul 23, 2015)

    • Added trimLeft, trimRight
    • Added support for unicode whitespace to trim
    • Added delimit
    • Added indexOf and indexOfLast
    • Added htmlEncode and htmlDecode
    • Added "Ç" in toAscii()
    Source code(tar.gz)
    Source code(zip)
  • 1.9.0(Feb 10, 2015)

    • Added hasUpperCase and hasLowerCase
    • Added $removeUnsupported parameter to toAscii()
    • Improved toAscii support with additional Unicode spaces, Vietnamese chars, and numerous other characters
    • Separated the charsArray from toAscii as a protected method that may be extended by inheriting classes
    • Chars array is cached for better performance
    Source code(tar.gz)
    Source code(zip)
  • 1.8.1(Jan 8, 2015)

    • Optimized chars()
    • Added "ä Ä Ö Ü"" in toAscii()
    • Added support for Unicode spaces in toAscii()
    • Replaced instances of self::create() with static::create()
    • Added missing test cases for safeTruncate() and longestCommonSuffix()
    • Updated Stringy\create() to avoid collision when it already exists
    Source code(tar.gz)
    Source code(zip)
  • 1.8.0(Jan 4, 2015)

  • 1.7.0(Oct 15, 2014)

  • 1.6.0(Sep 14, 2014)

  • 1.5.2(Jul 9, 2014)

  • 1.5.1(Apr 19, 2014)

    • Fixed toAscii() failing to remove remaining non-ascii characters
    • Updated slugify() to treat dash and underscore as delimiters by default
    • Updated slugify() to remove leading and trailing delimiter, if present
    Source code(tar.gz)
    Source code(zip)
  • 1.5.0(Mar 19, 2014)

    • Made both str and encoding protected, giving property access to subclasses
    • Added getEncoding()
    • Fixed isJSON() giving false negatives
    • Cleaned up and simplified: replace(), collapseWhitespace(), underscored(), dasherize(), pad(), padLeft(), padRight() and padBoth()
    • Fixed handling consecutive invalid chars in slugify()
    • Removed conflicting hard sign transliteration in toAscii()
    Source code(tar.gz)
    Source code(zip)
  • 1.4.0(Feb 13, 2014)

    • Implemented the IteratorAggregate interface, added chars()
    • Renamed count() to countSubstr()
    • Updated count() to implement Countable interface
    • Implemented the ArrayAccess interface with positive and negative indices
    • Switched from PSR-0 to PSR-4 autoloading
    Source code(tar.gz)
    Source code(zip)
  • 1.3.0(Dec 17, 2013)

    • Additional Bulgarian support for toAscii
    • str property made private
    • Constructor casts first argument to string
    • Constructor throws an InvalidArgumentException when given an array
    • Constructor throws an InvalidArgumentException when given an object without a __toString method
    Source code(tar.gz)
    Source code(zip)
  • 1.2.2(Dec 5, 2013)

Owner
Daniel St. Jules
Daniel St. Jules
:accept: Stringy - A PHP string manipulation library with multibyte support, performance optimized

?? Stringy A PHP string manipulation library with multibyte support. Compatible with PHP 7+ 100% compatible with the original "Stringy" library, but t

Lars Moelleken 144 Dec 12, 2022
ATOMASTIC 14 Mar 12, 2022
Multibyte strings as objects

Opis String Multibyte strings Opis String is a tiny library that allows you to work with multibyte encoded strings in an object-oriented manner. The l

Opis 58 Oct 6, 2022
Text - Simple 1 Class Text Manipulation Library

Text - Simple 1 Class Text Manipulation Library Do you remember PHP's string functions? If not, just wrap you text with Text! It will save a minute on

Kazuyuki Hayashi 51 Nov 16, 2021
🉑 Portable UTF-8 library - performance optimized (unicode) string functions for php.

?? Portable UTF-8 Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 474 Dec 22, 2022
🔡 Portable ASCII library - performance optimized (ascii) string functions for php.

?? Portable ASCII Description It is written in PHP (PHP 7+) and can work without "mbstring", "iconv" or any other extra encoding php-extension on your

Lars Moelleken 380 Jan 6, 2023
PHP library to parse urls from string input

Url highlight - PHP library to parse URLs from string input. Works with complex URLs, edge cases and encoded input. Features: Replace URLs in string b

Volodymyr Stelmakh 77 Sep 16, 2022
A language detection library for PHP. Detects the language from a given text string.

language-detection Build Status Code Coverage Version Total Downloads Minimum PHP Version License This library can detect the language of a given text

Patrick Schur 738 Dec 28, 2022
Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.

cocur/slugify Converts a string into a slug. Developed by Florian Eckerstorfer in Vienna, Europe with the help of many great contributors. Features Re

Cocur 2.8k Dec 22, 2022
Mobile_Detect is a lightweight PHP class for detecting mobile devices (including tablets). It uses the User-Agent string combined with specific HTTP headers to detect the mobile environment.

Motto: "Every business should have a detection script to detect mobile readers." About Mobile Detect is a lightweight PHP class for detecting mobile d

Şerban Ghiţă 10.2k Jan 4, 2023
Library for free use Google Translator. With attempts connecting on failure and array support.

GoogleTranslateForFree Packagist: https://packagist.org/packages/dejurin/php-google-translate-for-free Library for free use Google Translator. With at

Yurii De 122 Dec 23, 2022
👮 A PHP desktop/mobile user agent parser with support for Laravel, based on Mobiledetect

Agent A PHP desktop/mobile user agent parser with support for Laravel, based on Mobile Detect with desktop support and additional functionality. Insta

Jens Segers 4.2k Jan 5, 2023
PHP library to detect and manipulate indentation of strings and files

indentation PHP library to detect and manipulate the indentation of files and strings Installation composer require --dev colinodell/indentation Usage

Colin O'Dell 34 Nov 28, 2022
The Universal Device Detection library will parse any User Agent and detect the browser, operating system, device used (desktop, tablet, mobile, tv, cars, console, etc.), brand and model.

DeviceDetector Code Status Description The Universal Device Detection library that parses User Agents and detects devices (desktop, tablet, mobile, tv

Matomo Analytics 2.4k Jan 5, 2023
A fast PHP slug generator and transliteration library that converts non-ascii characters for use in URLs.

URLify for PHP A fast PHP slug generator and transliteration library, started as a PHP port of URLify.js from the Django project. Handles symbols from

Aband*nthecar 667 Dec 20, 2022
ColorJizz is a PHP library for manipulating and converting colors.

#Getting started: ColorJizz-PHP uses the PSR-0 standards for namespaces, so there should be no trouble using with frameworks like Symfony 2. ###Autolo

Mikeemoo 281 Nov 25, 2022
A PHP library for generating universally unique identifiers (UUIDs).

ramsey/uuid A PHP library for generating and working with UUIDs. ramsey/uuid is a PHP library for generating and working with universally unique ident

Ben Ramsey 11.9k Jan 8, 2023
The Hoa\Ustring library.

Hoa is a modular, extensible and structured set of PHP libraries. Moreover, Hoa aims at being a bridge between industrial and research worlds. Hoa\Ust

Hoa 402 Jan 4, 2023
PCRE wrapping library that offers type-safe preg_* replacements.

composer/pcre PCRE wrapping library that offers type-safe preg_* replacements. If you are using a modern PHP version you are probably better off using

Composer 308 Dec 30, 2022