mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-06 11:10:40 +02:00
Compare commits
28 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
4e214a5195 | ||
|
fcc2969210 | ||
|
30d1742082 | ||
|
bd90918858 | ||
|
a2d1ec535a | ||
|
b953a85d2c | ||
|
e07a07a076 | ||
|
c4d785d29f | ||
|
5554477962 | ||
|
87c1a47355 | ||
|
6d558974ba | ||
|
5a9b987f9a | ||
|
e3d397fa74 | ||
|
ffc3fa5ff9 | ||
|
c3f25577ed | ||
|
5a4629662c | ||
|
823e50a340 | ||
|
9d77c49fca | ||
|
a074d2a2ac | ||
|
b7cfa84219 | ||
|
e3be6a56ea | ||
|
bdbd4d7541 | ||
|
8419369f3b | ||
|
a3dde38a2f | ||
|
59ce593086 | ||
|
54cf92c89c | ||
|
d320bf8704 | ||
|
2de4c4aee3 |
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -1,5 +1,6 @@
|
|||||||
* text=auto
|
* text=auto
|
||||||
|
|
||||||
|
.editorconfig export-ignore
|
||||||
.gitattributes export-ignore
|
.gitattributes export-ignore
|
||||||
.gitignore export-ignore
|
.gitignore export-ignore
|
||||||
.travis.yml export-ignore
|
.travis.yml export-ignore
|
||||||
|
41
.travis.yml
41
.travis.yml
@@ -1,12 +1,43 @@
|
|||||||
language: php
|
language: php
|
||||||
|
|
||||||
php:
|
php:
|
||||||
- 5.6
|
|
||||||
- 5.5
|
|
||||||
- 5.4
|
|
||||||
- 5.3
|
|
||||||
- 7.0
|
|
||||||
- hhvm
|
- hhvm
|
||||||
|
|
||||||
# This triggers builds to run on the new TravisCI infrastructure.
|
# This triggers builds to run on the new TravisCI infrastructure.
|
||||||
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
|
||||||
sudo: false
|
sudo: false
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
fast_finish: true
|
||||||
|
include:
|
||||||
|
- php: 5.3
|
||||||
|
env: polyfill='true'
|
||||||
|
- php: 5.3
|
||||||
|
env: polyfill='false'
|
||||||
|
- php: 5.4
|
||||||
|
env: polyfill='true'
|
||||||
|
- php: 5.4
|
||||||
|
env: polyfill='false'
|
||||||
|
- php: 5.5
|
||||||
|
env: polyfill='true'
|
||||||
|
- php: 5.5
|
||||||
|
env: polyfill='false'
|
||||||
|
- php: 5.6
|
||||||
|
env: polyfill='true'
|
||||||
|
- php: 5.6
|
||||||
|
env: polyfill='false'
|
||||||
|
- php: 7.0
|
||||||
|
env: polyfill='true'
|
||||||
|
- php: 7.0
|
||||||
|
env: polyfill='false'
|
||||||
|
|
||||||
|
install: travis_retry composer install --no-interaction --prefer-source
|
||||||
|
|
||||||
|
before_script:
|
||||||
|
- travis_retry composer self-update
|
||||||
|
- travis_retry composer install --no-interaction --prefer-source
|
||||||
|
- if [[ $polyfill = 'false' ]]; then travis_retry composer remove symfony/polyfill-mbstring; fi
|
||||||
|
|
||||||
|
script:
|
||||||
|
- vendor/bin/phpunit
|
||||||
|
|
||||||
|
13
CHANGELOG.md
13
CHANGELOG.md
@@ -1,3 +1,16 @@
|
|||||||
|
### 2.3.2 (2016-05-02)
|
||||||
|
|
||||||
|
* Improve support without mbstring
|
||||||
|
|
||||||
|
### 2.3.1 (2016-03-21)
|
||||||
|
|
||||||
|
* Always use root namespace for mbstring functions
|
||||||
|
|
||||||
|
### 2.3.0 (2016-03-19)
|
||||||
|
|
||||||
|
* Add Persian characters in Stringy::charsArray()
|
||||||
|
* Use symfony/polyfill-mbstring to avoid dependency on ext-mbstring
|
||||||
|
|
||||||
### 2.2.0 (2015-12-20)
|
### 2.2.0 (2015-12-20)
|
||||||
|
|
||||||
* isJSON now returns false for empty strings
|
* isJSON now returns false for empty strings
|
||||||
|
27
README.md
27
README.md
@@ -9,6 +9,8 @@ s('string')->toTitleCase()->ensureRight('y') == 'Stringy'
|
|||||||
```
|
```
|
||||||
|
|
||||||
[](https://travis-ci.org/danielstjules/Stringy)
|
[](https://travis-ci.org/danielstjules/Stringy)
|
||||||
|
[](https://packagist.org/packages/danielstjules/stringy)
|
||||||
|
[](https://packagist.org/packages/danielstjules/stringy)
|
||||||
|
|
||||||
* [Why?](#why)
|
* [Why?](#why)
|
||||||
* [Installation](#installation)
|
* [Installation](#installation)
|
||||||
@@ -129,7 +131,7 @@ in your composer.json file:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
"require": {
|
"require": {
|
||||||
"danielstjules/stringy": "~2.2"
|
"danielstjules/stringy": "~2.3"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -152,11 +154,13 @@ And in either case, I'd suggest using an alias.
|
|||||||
use Stringy\Stringy as S;
|
use Stringy\Stringy as S;
|
||||||
```
|
```
|
||||||
|
|
||||||
Please note that Stringy relies on the `mbstring` PHP module for its underlying
|
Please note that Stringy relies on the `mbstring` module for its underlying
|
||||||
multibyte support. This is a non-default, but very common module. For example,
|
multibyte support. If the module is not found, Stringy will use
|
||||||
with debian and ubuntu, it's included in libapache2-mod-php5, php5-cli, and
|
[symfony/polyfill-mbstring](https://github.com/symfony/polyfill-mbstring).
|
||||||
php5-fpm. For OSX users, it's a default for any version of PHP installed with
|
ex-mbstring is a non-default, but very common module. For example, with debian
|
||||||
homebrew. If compiling PHP from scratch, it can be included with the
|
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.
|
`--enable-mbstring` flag.
|
||||||
|
|
||||||
## OO and Chaining
|
## OO and Chaining
|
||||||
@@ -235,9 +239,9 @@ integer, boolean, etc.
|
|||||||
```php
|
```php
|
||||||
use Stringy\StaticStringy as S;
|
use Stringy\StaticStringy as S;
|
||||||
|
|
||||||
// Translates to Stringy::create('fòôbàř', 'UTF-8')->slice(0, 3);
|
// Translates to Stringy::create('fòôbàř')->slice(0, 3);
|
||||||
// Returns a Stringy object with the string "fòô"
|
// Returns a Stringy object with the string "fòô"
|
||||||
S::slice('fòôbàř', 0, 3, 'UTF-8');
|
S::slice('fòôbàř', 0, 3);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Class methods
|
## Class methods
|
||||||
@@ -251,7 +255,7 @@ then returns the initialized object. Throws an InvalidArgumentException
|
|||||||
if the first argument is an array or object without a __toString method.
|
if the first argument is an array or object without a __toString method.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
$stringy = S::create('fòôbàř', 'UTF-8'); // 'fòôbàř'
|
$stringy = S::create('fòôbàř'); // 'fòôbàř'
|
||||||
```
|
```
|
||||||
|
|
||||||
## Instance Methods
|
## Instance Methods
|
||||||
@@ -416,7 +420,7 @@ s('fòôbàř')->first(3); // 'fòô'
|
|||||||
Returns the encoding used by the Stringy object.
|
Returns the encoding used by the Stringy object.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
s('fòôbàř', 'UTF-8')->getEncoding(); // 'UTF-8'
|
s('fòôbàř')->getEncoding(); // 'UTF-8'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### hasLowerCase()
|
##### hasLowerCase()
|
||||||
@@ -682,7 +686,8 @@ to a lack of support in the bundled version of Oniguruma in PHP < 5.6,
|
|||||||
and current versions of HHVM (3.8 and below).
|
and current versions of HHVM (3.8 and below).
|
||||||
|
|
||||||
```php
|
```php
|
||||||
s('fòô ')->regexReplace('f[òô]+\s', 'bàř', 'msr'); // 'bàř'
|
s('fòô ')->regexReplace('f[òô]+\s', 'bàř'); // 'bàř'
|
||||||
|
s('fò')->regexReplace('(ò)', '\\1ô'); // 'fòô'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### removeLeft(string $substring)
|
##### removeLeft(string $substring)
|
||||||
|
@@ -16,7 +16,7 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0",
|
"php": ">=5.3.0",
|
||||||
"ext-mbstring": "*"
|
"symfony/polyfill-mbstring": "~1.1"
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
"phpunit/phpunit": "~4.0"
|
"phpunit/phpunit": "~4.0"
|
||||||
|
@@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<phpunit processIsolation="false"
|
<phpunit bootstrap="vendor/autoload.php"
|
||||||
|
processIsolation="false"
|
||||||
stopOnFailure="false"
|
stopOnFailure="false"
|
||||||
syntaxCheck="false">
|
syntaxCheck="false">
|
||||||
<testsuites>
|
<testsuites>
|
||||||
|
@@ -2,6 +2,10 @@
|
|||||||
|
|
||||||
namespace Stringy;
|
namespace Stringy;
|
||||||
|
|
||||||
|
use BadMethodCallException;
|
||||||
|
use ReflectionClass;
|
||||||
|
use ReflectionMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class StaticStringy
|
* Class StaticStringy
|
||||||
*
|
*
|
||||||
@@ -107,12 +111,14 @@ class StaticStringy
|
|||||||
* @param mixed[] $arguments
|
* @param mixed[] $arguments
|
||||||
*
|
*
|
||||||
* @return Stringy
|
* @return Stringy
|
||||||
|
*
|
||||||
|
* @throws \BadMethodCallException
|
||||||
*/
|
*/
|
||||||
public static function __callStatic($name, $arguments)
|
public static function __callStatic($name, $arguments)
|
||||||
{
|
{
|
||||||
if (!static::$methodArgs) {
|
if (!static::$methodArgs) {
|
||||||
$stringyClass = new \ReflectionClass('Stringy\Stringy');
|
$stringyClass = new ReflectionClass('Stringy\Stringy');
|
||||||
$methods = $stringyClass->getMethods(\ReflectionMethod::IS_PUBLIC);
|
$methods = $stringyClass->getMethods(ReflectionMethod::IS_PUBLIC);
|
||||||
|
|
||||||
foreach ($methods as $method) {
|
foreach ($methods as $method) {
|
||||||
$params = $method->getNumberOfParameters() + 2;
|
$params = $method->getNumberOfParameters() + 2;
|
||||||
@@ -121,7 +127,7 @@ class StaticStringy
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isset(static::$methodArgs[$name])) {
|
if (!isset(static::$methodArgs[$name])) {
|
||||||
throw new \BadMethodCallException($name . ' is not a valid method');
|
throw new BadMethodCallException($name . ' is not a valid method');
|
||||||
}
|
}
|
||||||
|
|
||||||
$numArgs = count($arguments);
|
$numArgs = count($arguments);
|
||||||
|
321
src/Stringy.php
321
src/Stringy.php
@@ -2,7 +2,15 @@
|
|||||||
|
|
||||||
namespace Stringy;
|
namespace Stringy;
|
||||||
|
|
||||||
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
use ArrayAccess;
|
||||||
|
use ArrayIterator;
|
||||||
|
use Countable;
|
||||||
|
use Exception;
|
||||||
|
use InvalidArgumentException;
|
||||||
|
use IteratorAggregate;
|
||||||
|
use OutOfBoundsException;
|
||||||
|
|
||||||
|
class Stringy implements Countable, IteratorAggregate, ArrayAccess
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* An instance's string.
|
* An instance's string.
|
||||||
@@ -34,17 +42,17 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function __construct($str = '', $encoding = null)
|
public function __construct($str = '', $encoding = null)
|
||||||
{
|
{
|
||||||
if (is_array($str)) {
|
if (is_array($str)) {
|
||||||
throw new \InvalidArgumentException(
|
throw new InvalidArgumentException(
|
||||||
'Passed value cannot be an array'
|
'Passed value cannot be an array'
|
||||||
);
|
);
|
||||||
} elseif (is_object($str) && !method_exists($str, '__toString')) {
|
} elseif (is_object($str) && !method_exists($str, '__toString')) {
|
||||||
throw new \InvalidArgumentException(
|
throw new InvalidArgumentException(
|
||||||
'Passed object must have a __toString method'
|
'Passed object must have a __toString method'
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->str = (string) $str;
|
$this->str = (string) $str;
|
||||||
$this->encoding = $encoding ?: mb_internal_encoding();
|
$this->encoding = $encoding ?: \mb_internal_encoding();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,7 +122,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
return static::create('', $this->encoding);
|
return static::create('', $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
$substrIndex = $startIndex + mb_strlen($start, $this->encoding);
|
$substrIndex = $startIndex + \mb_strlen($start, $this->encoding);
|
||||||
$endIndex = $this->indexOf($end, $substrIndex);
|
$endIndex = $this->indexOf($end, $substrIndex);
|
||||||
if ($endIndex === false) {
|
if ($endIndex === false) {
|
||||||
return static::create('', $this->encoding);
|
return static::create('', $this->encoding);
|
||||||
@@ -140,10 +148,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'/[-_\s]+(.)?/u',
|
'/[-_\s]+(.)?/u',
|
||||||
function ($match) use ($encoding) {
|
function ($match) use ($encoding) {
|
||||||
if (isset($match[1])) {
|
if (isset($match[1])) {
|
||||||
return mb_strtoupper($match[1], $encoding);
|
return \mb_strtoupper($match[1], $encoding);
|
||||||
} else {
|
|
||||||
return '';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return '';
|
||||||
},
|
},
|
||||||
$stringy->str
|
$stringy->str
|
||||||
);
|
);
|
||||||
@@ -151,7 +159,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$stringy->str = preg_replace_callback(
|
$stringy->str = preg_replace_callback(
|
||||||
'/[\d]+(.)?/u',
|
'/[\d]+(.)?/u',
|
||||||
function ($match) use ($encoding) {
|
function ($match) use ($encoding) {
|
||||||
return mb_strtoupper($match[0], $encoding);
|
return \mb_strtoupper($match[0], $encoding);
|
||||||
},
|
},
|
||||||
$stringy->str
|
$stringy->str
|
||||||
);
|
);
|
||||||
@@ -200,10 +208,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$encoding = $this->encoding;
|
$encoding = $this->encoding;
|
||||||
|
|
||||||
if ($caseSensitive) {
|
if ($caseSensitive) {
|
||||||
return (mb_strpos($this->str, $needle, 0, $encoding) !== false);
|
return (\mb_strpos($this->str, $needle, 0, $encoding) !== false);
|
||||||
} else {
|
|
||||||
return (mb_stripos($this->str, $needle, 0, $encoding) !== false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (\mb_stripos($this->str, $needle, 0, $encoding) !== false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -276,13 +284,13 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function countSubstr($substring, $caseSensitive = true)
|
public function countSubstr($substring, $caseSensitive = true)
|
||||||
{
|
{
|
||||||
if ($caseSensitive) {
|
if ($caseSensitive) {
|
||||||
return mb_substr_count($this->str, $substring, $this->encoding);
|
return \mb_substr_count($this->str, $substring, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
$str = mb_strtoupper($this->str, $this->encoding);
|
$str = \mb_strtoupper($this->str, $this->encoding);
|
||||||
$substring = mb_strtoupper($substring, $this->encoding);
|
$substring = \mb_strtoupper($substring, $this->encoding);
|
||||||
|
|
||||||
return mb_substr_count($str, $substring, $this->encoding);
|
return \mb_substr_count($str, $substring, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -308,15 +316,14 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function delimit($delimiter)
|
public function delimit($delimiter)
|
||||||
{
|
{
|
||||||
// Save current regex encoding so we can reset it after
|
$regexEncoding = $this->regexEncoding();
|
||||||
$regexEncoding = mb_regex_encoding();
|
$this->regexEncoding($this->encoding);
|
||||||
mb_regex_encoding($this->encoding);
|
|
||||||
|
|
||||||
$str = mb_ereg_replace('\B([A-Z])', '-\1', $this->trim());
|
$str = $this->eregReplace('\B([A-Z])', '-\1', $this->trim());
|
||||||
$str = mb_strtolower($str, $this->encoding);
|
$str = \mb_strtolower($str, $this->encoding);
|
||||||
$str = mb_ereg_replace('[-_\s]+', $delimiter, $str);
|
$str = $this->eregReplace('[-_\s]+', $delimiter, $str);
|
||||||
|
|
||||||
mb_regex_encoding($regexEncoding);
|
$this->regexEncoding($regexEncoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -332,15 +339,15 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function endsWith($substring, $caseSensitive = true)
|
public function endsWith($substring, $caseSensitive = true)
|
||||||
{
|
{
|
||||||
$substringLength = mb_strlen($substring, $this->encoding);
|
$substringLength = \mb_strlen($substring, $this->encoding);
|
||||||
$strLength = $this->length();
|
$strLength = $this->length();
|
||||||
|
|
||||||
$endOfStr = mb_substr($this->str, $strLength - $substringLength,
|
$endOfStr = \mb_substr($this->str, $strLength - $substringLength,
|
||||||
$substringLength, $this->encoding);
|
$substringLength, $this->encoding);
|
||||||
|
|
||||||
if (!$caseSensitive) {
|
if (!$caseSensitive) {
|
||||||
$substring = mb_strtolower($substring, $this->encoding);
|
$substring = \mb_strtolower($substring, $this->encoding);
|
||||||
$endOfStr = mb_strtolower($endOfStr, $this->encoding);
|
$endOfStr = \mb_strtolower($endOfStr, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string) $substring === $endOfStr;
|
return (string) $substring === $endOfStr;
|
||||||
@@ -394,11 +401,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
if ($n < 0) {
|
if ($n < 0) {
|
||||||
$stringy->str = '';
|
$stringy->str = '';
|
||||||
} else {
|
return $stringy;
|
||||||
return $stringy->substr(0, $n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $stringy;
|
return $stringy->substr(0, $n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -421,7 +427,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function getIterator()
|
public function getIterator()
|
||||||
{
|
{
|
||||||
return new \ArrayIterator($this->chars());
|
return new ArrayIterator($this->chars());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -501,7 +507,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function indexOf($needle, $offset = 0)
|
public function indexOf($needle, $offset = 0)
|
||||||
{
|
{
|
||||||
return mb_strpos($this->str, (string) $needle,
|
return \mb_strpos($this->str, (string) $needle,
|
||||||
(int) $offset, $this->encoding);
|
(int) $offset, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -517,7 +523,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function indexOfLast($needle, $offset = 0)
|
public function indexOfLast($needle, $offset = 0)
|
||||||
{
|
{
|
||||||
return mb_strrpos($this->str, (string) $needle,
|
return \mb_strrpos($this->str, (string) $needle,
|
||||||
(int) $offset, $this->encoding);
|
(int) $offset, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -535,8 +541,8 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
return $stringy;
|
return $stringy;
|
||||||
}
|
}
|
||||||
|
|
||||||
$start = mb_substr($stringy->str, 0, $index, $stringy->encoding);
|
$start = \mb_substr($stringy->str, 0, $index, $stringy->encoding);
|
||||||
$end = mb_substr($stringy->str, $index, $stringy->length(),
|
$end = \mb_substr($stringy->str, $index, $stringy->length(),
|
||||||
$stringy->encoding);
|
$stringy->encoding);
|
||||||
|
|
||||||
$stringy->str = $start . $substring . $end;
|
$stringy->str = $start . $substring . $end;
|
||||||
@@ -661,11 +667,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
if ($n <= 0) {
|
if ($n <= 0) {
|
||||||
$stringy->str = '';
|
$stringy->str = '';
|
||||||
} else {
|
return $stringy;
|
||||||
return $stringy->substr(-$n);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $stringy;
|
return $stringy->substr(-$n);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -675,7 +680,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function length()
|
public function length()
|
||||||
{
|
{
|
||||||
return mb_strlen($this->str, $this->encoding);
|
return \mb_strlen($this->str, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -686,7 +691,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function lines()
|
public function lines()
|
||||||
{
|
{
|
||||||
$array = mb_split('[\r\n]{1,2}', $this->str);
|
$array = $this->split('[\r\n]{1,2}', $this->str);
|
||||||
for ($i = 0; $i < count($array); $i++) {
|
for ($i = 0; $i < count($array); $i++) {
|
||||||
$array[$i] = static::create($array[$i], $this->encoding);
|
$array[$i] = static::create($array[$i], $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -703,13 +708,13 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function longestCommonPrefix($otherStr)
|
public function longestCommonPrefix($otherStr)
|
||||||
{
|
{
|
||||||
$encoding = $this->encoding;
|
$encoding = $this->encoding;
|
||||||
$maxLength = min($this->length(), mb_strlen($otherStr, $encoding));
|
$maxLength = min($this->length(), \mb_strlen($otherStr, $encoding));
|
||||||
|
|
||||||
$longestCommonPrefix = '';
|
$longestCommonPrefix = '';
|
||||||
for ($i = 0; $i < $maxLength; $i++) {
|
for ($i = 0; $i < $maxLength; $i++) {
|
||||||
$char = mb_substr($this->str, $i, 1, $encoding);
|
$char = \mb_substr($this->str, $i, 1, $encoding);
|
||||||
|
|
||||||
if ($char == mb_substr($otherStr, $i, 1, $encoding)) {
|
if ($char == \mb_substr($otherStr, $i, 1, $encoding)) {
|
||||||
$longestCommonPrefix .= $char;
|
$longestCommonPrefix .= $char;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -728,13 +733,13 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function longestCommonSuffix($otherStr)
|
public function longestCommonSuffix($otherStr)
|
||||||
{
|
{
|
||||||
$encoding = $this->encoding;
|
$encoding = $this->encoding;
|
||||||
$maxLength = min($this->length(), mb_strlen($otherStr, $encoding));
|
$maxLength = min($this->length(), \mb_strlen($otherStr, $encoding));
|
||||||
|
|
||||||
$longestCommonSuffix = '';
|
$longestCommonSuffix = '';
|
||||||
for ($i = 1; $i <= $maxLength; $i++) {
|
for ($i = 1; $i <= $maxLength; $i++) {
|
||||||
$char = mb_substr($this->str, -$i, 1, $encoding);
|
$char = \mb_substr($this->str, -$i, 1, $encoding);
|
||||||
|
|
||||||
if ($char == mb_substr($otherStr, -$i, 1, $encoding)) {
|
if ($char == \mb_substr($otherStr, -$i, 1, $encoding)) {
|
||||||
$longestCommonSuffix = $char . $longestCommonSuffix;
|
$longestCommonSuffix = $char . $longestCommonSuffix;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@@ -758,7 +763,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$encoding = $this->encoding;
|
$encoding = $this->encoding;
|
||||||
$stringy = static::create($this->str, $encoding);
|
$stringy = static::create($this->str, $encoding);
|
||||||
$strLength = $stringy->length();
|
$strLength = $stringy->length();
|
||||||
$otherLength = mb_strlen($otherStr, $encoding);
|
$otherLength = \mb_strlen($otherStr, $encoding);
|
||||||
|
|
||||||
// Return if either string is empty
|
// Return if either string is empty
|
||||||
if ($strLength == 0 || $otherLength == 0) {
|
if ($strLength == 0 || $otherLength == 0) {
|
||||||
@@ -773,8 +778,8 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
for ($i = 1; $i <= $strLength; $i++) {
|
for ($i = 1; $i <= $strLength; $i++) {
|
||||||
for ($j = 1; $j <= $otherLength; $j++) {
|
for ($j = 1; $j <= $otherLength; $j++) {
|
||||||
$strChar = mb_substr($stringy->str, $i - 1, 1, $encoding);
|
$strChar = \mb_substr($stringy->str, $i - 1, 1, $encoding);
|
||||||
$otherChar = mb_substr($otherStr, $j - 1, 1, $encoding);
|
$otherChar = \mb_substr($otherStr, $j - 1, 1, $encoding);
|
||||||
|
|
||||||
if ($strChar == $otherChar) {
|
if ($strChar == $otherChar) {
|
||||||
$table[$i][$j] = $table[$i - 1][$j - 1] + 1;
|
$table[$i][$j] = $table[$i - 1][$j - 1] + 1;
|
||||||
@@ -788,7 +793,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$stringy->str = mb_substr($stringy->str, $end - $len, $len, $encoding);
|
$stringy->str = \mb_substr($stringy->str, $end - $len, $len, $encoding);
|
||||||
|
|
||||||
return $stringy;
|
return $stringy;
|
||||||
}
|
}
|
||||||
@@ -800,11 +805,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function lowerCaseFirst()
|
public function lowerCaseFirst()
|
||||||
{
|
{
|
||||||
$first = mb_substr($this->str, 0, 1, $this->encoding);
|
$first = \mb_substr($this->str, 0, 1, $this->encoding);
|
||||||
$rest = mb_substr($this->str, 1, $this->length() - 1,
|
$rest = \mb_substr($this->str, 1, $this->length() - 1,
|
||||||
$this->encoding);
|
$this->encoding);
|
||||||
|
|
||||||
$str = mb_strtolower($first, $this->encoding) . $rest;
|
$str = \mb_strtolower($first, $this->encoding) . $rest;
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -846,10 +851,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$length = $this->length();
|
$length = $this->length();
|
||||||
|
|
||||||
if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) {
|
if (($offset >= 0 && $length <= $offset) || $length < abs($offset)) {
|
||||||
throw new \OutOfBoundsException('No character exists at the index');
|
throw new OutOfBoundsException('No character exists at the index');
|
||||||
}
|
}
|
||||||
|
|
||||||
return mb_substr($this->str, $offset, 1, $this->encoding);
|
return \mb_substr($this->str, $offset, 1, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -863,7 +868,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function offsetSet($offset, $value)
|
public function offsetSet($offset, $value)
|
||||||
{
|
{
|
||||||
// Stringy is immutable, cannot directly set char
|
// Stringy is immutable, cannot directly set char
|
||||||
throw new \Exception('Stringy object is immutable, cannot modify char');
|
throw new Exception('Stringy object is immutable, cannot modify char');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -876,7 +881,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function offsetUnset($offset)
|
public function offsetUnset($offset)
|
||||||
{
|
{
|
||||||
// Don't allow directly modifying the string
|
// Don't allow directly modifying the string
|
||||||
throw new \Exception('Stringy object is immutable, cannot unset char');
|
throw new Exception('Stringy object is immutable, cannot unset char');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -896,7 +901,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function pad($length, $padStr = ' ', $padType = 'right')
|
public function pad($length, $padStr = ' ', $padType = 'right')
|
||||||
{
|
{
|
||||||
if (!in_array($padType, array('left', 'right', 'both'))) {
|
if (!in_array($padType, array('left', 'right', 'both'))) {
|
||||||
throw new \InvalidArgumentException('Pad expects $padType ' .
|
throw new InvalidArgumentException('Pad expects $padType ' .
|
||||||
"to be one of 'left', 'right' or 'both'");
|
"to be one of 'left', 'right' or 'both'");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -977,11 +982,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function regexReplace($pattern, $replacement, $options = 'msr')
|
public function regexReplace($pattern, $replacement, $options = 'msr')
|
||||||
{
|
{
|
||||||
$regexEncoding = mb_regex_encoding();
|
$regexEncoding = $this->regexEncoding();
|
||||||
mb_regex_encoding($this->encoding);
|
$this->regexEncoding($this->encoding);
|
||||||
|
|
||||||
$str = mb_ereg_replace($pattern, $replacement, $this->str, $options);
|
$str = $this->eregReplace($pattern, $replacement, $this->str, $options);
|
||||||
mb_regex_encoding($regexEncoding);
|
$this->regexEncoding($regexEncoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -997,7 +1002,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$stringy = static::create($this->str, $this->encoding);
|
$stringy = static::create($this->str, $this->encoding);
|
||||||
|
|
||||||
if ($stringy->startsWith($substring)) {
|
if ($stringy->startsWith($substring)) {
|
||||||
$substringLength = mb_strlen($substring, $stringy->encoding);
|
$substringLength = \mb_strlen($substring, $stringy->encoding);
|
||||||
return $stringy->substr($substringLength);
|
return $stringy->substr($substringLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1015,7 +1020,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$stringy = static::create($this->str, $this->encoding);
|
$stringy = static::create($this->str, $this->encoding);
|
||||||
|
|
||||||
if ($stringy->endsWith($substring)) {
|
if ($stringy->endsWith($substring)) {
|
||||||
$substringLength = mb_strlen($substring, $stringy->encoding);
|
$substringLength = \mb_strlen($substring, $stringy->encoding);
|
||||||
return $stringy->substr(0, $stringy->length() - $substringLength);
|
return $stringy->substr(0, $stringy->length() - $substringLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1059,7 +1064,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
// Loop from last index of string to first
|
// Loop from last index of string to first
|
||||||
for ($i = $strLength - 1; $i >= 0; $i--) {
|
for ($i = $strLength - 1; $i >= 0; $i--) {
|
||||||
$reversed .= mb_substr($this->str, $i, 1, $this->encoding);
|
$reversed .= \mb_substr($this->str, $i, 1, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::create($reversed, $this->encoding);
|
return static::create($reversed, $this->encoding);
|
||||||
@@ -1084,16 +1089,16 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
// Need to further trim the string so we can append the substring
|
// Need to further trim the string so we can append the substring
|
||||||
$encoding = $stringy->encoding;
|
$encoding = $stringy->encoding;
|
||||||
$substringLength = mb_strlen($substring, $encoding);
|
$substringLength = \mb_strlen($substring, $encoding);
|
||||||
$length = $length - $substringLength;
|
$length = $length - $substringLength;
|
||||||
|
|
||||||
$truncated = mb_substr($stringy->str, 0, $length, $encoding);
|
$truncated = \mb_substr($stringy->str, 0, $length, $encoding);
|
||||||
|
|
||||||
// If the last word was truncated
|
// If the last word was truncated
|
||||||
if (mb_strpos($stringy->str, ' ', $length - 1, $encoding) != $length) {
|
if (mb_strpos($stringy->str, ' ', $length - 1, $encoding) != $length) {
|
||||||
// Find pos of the last occurrence of a space, get up to that
|
// Find pos of the last occurrence of a space, get up to that
|
||||||
$lastPos = mb_strrpos($truncated, ' ', 0, $encoding);
|
$lastPos = \mb_strrpos($truncated, ' ', 0, $encoding);
|
||||||
$truncated = mb_substr($truncated, 0, $lastPos, $encoding);
|
$truncated = \mb_substr($truncated, 0, $lastPos, $encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stringy->str = $truncated . $substring;
|
$stringy->str = $truncated . $substring;
|
||||||
@@ -1114,7 +1119,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
|
|
||||||
$shuffledStr = '';
|
$shuffledStr = '';
|
||||||
foreach ($indexes as $i) {
|
foreach ($indexes as $i) {
|
||||||
$shuffledStr .= mb_substr($this->str, $i, 1, $this->encoding);
|
$shuffledStr .= \mb_substr($this->str, $i, 1, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
return static::create($shuffledStr, $this->encoding);
|
return static::create($shuffledStr, $this->encoding);
|
||||||
@@ -1153,13 +1158,13 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function startsWith($substring, $caseSensitive = true)
|
public function startsWith($substring, $caseSensitive = true)
|
||||||
{
|
{
|
||||||
$substringLength = mb_strlen($substring, $this->encoding);
|
$substringLength = \mb_strlen($substring, $this->encoding);
|
||||||
$startOfStr = mb_substr($this->str, 0, $substringLength,
|
$startOfStr = \mb_substr($this->str, 0, $substringLength,
|
||||||
$this->encoding);
|
$this->encoding);
|
||||||
|
|
||||||
if (!$caseSensitive) {
|
if (!$caseSensitive) {
|
||||||
$substring = mb_strtolower($substring, $this->encoding);
|
$substring = \mb_strtolower($substring, $this->encoding);
|
||||||
$startOfStr = mb_strtolower($startOfStr, $this->encoding);
|
$startOfStr = \mb_strtolower($startOfStr, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (string) $substring === $startOfStr;
|
return (string) $substring === $startOfStr;
|
||||||
@@ -1187,7 +1192,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$length = $end - $start;
|
$length = $end - $start;
|
||||||
}
|
}
|
||||||
|
|
||||||
$str = mb_substr($this->str, $start, $length, $this->encoding);
|
$str = \mb_substr($this->str, $start, $length, $this->encoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -1208,19 +1213,30 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mb_split errors when supplied an empty pattern in < PHP 5.4.13
|
// mb_split errors when supplied an empty pattern in < PHP 5.4.13
|
||||||
// and current versions of HHVM (3.8 and below)
|
// and HHVM < 3.8
|
||||||
if ($pattern === '') {
|
if ($pattern === '') {
|
||||||
return array(static::create($this->str, $this->encoding));
|
return array(static::create($this->str, $this->encoding));
|
||||||
}
|
}
|
||||||
|
|
||||||
$regexEncoding = mb_regex_encoding();
|
$regexEncoding = $this->regexEncoding();
|
||||||
mb_regex_encoding($this->encoding);
|
$this->regexEncoding($this->encoding);
|
||||||
|
|
||||||
// mb_split returns the remaining unsplit string in the last index when
|
// mb_split returns the remaining unsplit string in the last index when
|
||||||
// supplying a limit
|
// supplying a limit
|
||||||
$limit = ($limit > 0) ? $limit += 1 : -1;
|
$limit = ($limit > 0) ? $limit += 1 : -1;
|
||||||
$array = mb_split($pattern, $this->str, $limit);
|
|
||||||
mb_regex_encoding($regexEncoding);
|
static $functionExists;
|
||||||
|
if ($functionExists === null) {
|
||||||
|
$functionExists = function_exists('\mb_split');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($functionExists) {
|
||||||
|
$array = \mb_split($pattern, $this->str, $limit);
|
||||||
|
} else if ($this->supportsEncoding()) {
|
||||||
|
$array = \preg_split("/$pattern/", $this->str, $limit);
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->regexEncoding($regexEncoding);
|
||||||
|
|
||||||
if ($limit > 0 && count($array) === $limit) {
|
if ($limit > 0 && count($array) === $limit) {
|
||||||
array_pop($array);
|
array_pop($array);
|
||||||
@@ -1245,7 +1261,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
public function substr($start, $length = null)
|
public function substr($start, $length = null)
|
||||||
{
|
{
|
||||||
$length = $length === null ? $this->length() : $length;
|
$length = $length === null ? $this->length() : $length;
|
||||||
$str = mb_substr($this->str, $start, $length, $this->encoding);
|
$str = \mb_substr($this->str, $start, $length, $this->encoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -1277,11 +1293,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$stringy->str = preg_replace_callback(
|
$stringy->str = preg_replace_callback(
|
||||||
'/[\S]/u',
|
'/[\S]/u',
|
||||||
function ($match) use ($encoding) {
|
function ($match) use ($encoding) {
|
||||||
if ($match[0] == mb_strtoupper($match[0], $encoding)) {
|
if ($match[0] == \mb_strtoupper($match[0], $encoding)) {
|
||||||
return mb_strtolower($match[0], $encoding);
|
return \mb_strtolower($match[0], $encoding);
|
||||||
} else {
|
|
||||||
return mb_strtoupper($match[0], $encoding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return \mb_strtoupper($match[0], $encoding);
|
||||||
},
|
},
|
||||||
$stringy->str
|
$stringy->str
|
||||||
);
|
);
|
||||||
@@ -1331,10 +1347,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
function ($match) use ($encoding, $ignore) {
|
function ($match) use ($encoding, $ignore) {
|
||||||
if ($ignore && in_array($match[0], $ignore)) {
|
if ($ignore && in_array($match[0], $ignore)) {
|
||||||
return $match[0];
|
return $match[0];
|
||||||
} else {
|
|
||||||
$stringy = new Stringy($match[0], $encoding);
|
|
||||||
return (string) $stringy->toLowerCase()->upperCaseFirst();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$stringy = new Stringy($match[0], $encoding);
|
||||||
|
|
||||||
|
return (string) $stringy->toLowerCase()->upperCaseFirst();
|
||||||
},
|
},
|
||||||
$stringy->str
|
$stringy->str
|
||||||
);
|
);
|
||||||
@@ -1395,9 +1412,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
return $map[$key];
|
return $map[$key];
|
||||||
} elseif (is_numeric($this->str)) {
|
} elseif (is_numeric($this->str)) {
|
||||||
return (intval($this->str) > 0);
|
return (intval($this->str) > 0);
|
||||||
} else {
|
|
||||||
return (bool) $this->regexReplace('[[:space:]]', '')->str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (bool) $this->regexReplace('[[:space:]]', '')->str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1408,7 +1425,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function toLowerCase()
|
public function toLowerCase()
|
||||||
{
|
{
|
||||||
$str = mb_strtolower($this->str, $this->encoding);
|
$str = \mb_strtolower($this->str, $this->encoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -1451,7 +1468,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function toTitleCase()
|
public function toTitleCase()
|
||||||
{
|
{
|
||||||
$str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding);
|
$str = \mb_convert_case($this->str, \MB_CASE_TITLE, $this->encoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -1464,7 +1481,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function toUpperCase()
|
public function toUpperCase()
|
||||||
{
|
{
|
||||||
$str = mb_strtoupper($this->str, $this->encoding);
|
$str = \mb_strtoupper($this->str, $this->encoding);
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -1531,10 +1548,10 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Need to further trim the string so we can append the substring
|
// Need to further trim the string so we can append the substring
|
||||||
$substringLength = mb_strlen($substring, $stringy->encoding);
|
$substringLength = \mb_strlen($substring, $stringy->encoding);
|
||||||
$length = $length - $substringLength;
|
$length = $length - $substringLength;
|
||||||
|
|
||||||
$truncated = mb_substr($stringy->str, 0, $length, $stringy->encoding);
|
$truncated = \mb_substr($stringy->str, 0, $length, $stringy->encoding);
|
||||||
$stringy->str = $truncated . $substring;
|
$stringy->str = $truncated . $substring;
|
||||||
|
|
||||||
return $stringy;
|
return $stringy;
|
||||||
@@ -1572,11 +1589,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function upperCaseFirst()
|
public function upperCaseFirst()
|
||||||
{
|
{
|
||||||
$first = mb_substr($this->str, 0, 1, $this->encoding);
|
$first = \mb_substr($this->str, 0, 1, $this->encoding);
|
||||||
$rest = mb_substr($this->str, 1, $this->length() - 1,
|
$rest = \mb_substr($this->str, 1, $this->length() - 1,
|
||||||
$this->encoding);
|
$this->encoding);
|
||||||
|
|
||||||
$str = mb_strtoupper($first, $this->encoding) . $rest;
|
$str = \mb_strtoupper($first, $this->encoding) . $rest;
|
||||||
|
|
||||||
return static::create($str, $this->encoding);
|
return static::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
@@ -1592,22 +1609,22 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
if (isset($charsArray)) return $charsArray;
|
if (isset($charsArray)) return $charsArray;
|
||||||
|
|
||||||
return $charsArray = array(
|
return $charsArray = array(
|
||||||
'0' => array('°', '₀'),
|
'0' => array('°', '₀', '۰'),
|
||||||
'1' => array('¹', '₁'),
|
'1' => array('¹', '₁', '۱'),
|
||||||
'2' => array('²', '₂'),
|
'2' => array('²', '₂', '۲'),
|
||||||
'3' => array('³', '₃'),
|
'3' => array('³', '₃', '۳'),
|
||||||
'4' => array('⁴', '₄'),
|
'4' => array('⁴', '₄', '۴', '٤'),
|
||||||
'5' => array('⁵', '₅'),
|
'5' => array('⁵', '₅', '۵', '٥'),
|
||||||
'6' => array('⁶', '₆'),
|
'6' => array('⁶', '₆', '۶', '٦'),
|
||||||
'7' => array('⁷', '₇'),
|
'7' => array('⁷', '₇', '۷'),
|
||||||
'8' => array('⁸', '₈'),
|
'8' => array('⁸', '₈', '۸'),
|
||||||
'9' => array('⁹', '₉'),
|
'9' => array('⁹', '₉', '۹'),
|
||||||
'a' => array('à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ',
|
'a' => array('à', 'á', 'ả', 'ã', 'ạ', 'ă', 'ắ', 'ằ', 'ẳ', 'ẵ',
|
||||||
'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å',
|
'ặ', 'â', 'ấ', 'ầ', 'ẩ', 'ẫ', 'ậ', 'ā', 'ą', 'å',
|
||||||
'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ',
|
'α', 'ά', 'ἀ', 'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ',
|
||||||
'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά',
|
'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ', 'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά',
|
||||||
'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ',
|
'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ', 'ᾶ', 'ᾷ', 'а', 'أ', 'အ',
|
||||||
'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ'),
|
'ာ', 'ါ', 'ǻ', 'ǎ', 'ª', 'ა', 'अ', 'ا'),
|
||||||
'b' => array('б', 'β', 'Ъ', 'Ь', 'ب', 'ဗ', 'ბ'),
|
'b' => array('б', 'β', 'Ъ', 'Ь', 'ب', 'ဗ', 'ბ'),
|
||||||
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
|
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
|
||||||
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ',
|
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ',
|
||||||
@@ -1615,17 +1632,18 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'e' => array('é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ',
|
'e' => array('é', 'è', 'ẻ', 'ẽ', 'ẹ', 'ê', 'ế', 'ề', 'ể', 'ễ',
|
||||||
'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ',
|
'ệ', 'ë', 'ē', 'ę', 'ě', 'ĕ', 'ė', 'ε', 'έ', 'ἐ',
|
||||||
'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э',
|
'ἑ', 'ἒ', 'ἓ', 'ἔ', 'ἕ', 'ὲ', 'έ', 'е', 'ё', 'э',
|
||||||
'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए'),
|
'є', 'ə', 'ဧ', 'ေ', 'ဲ', 'ე', 'ए', 'إ', 'ئ'),
|
||||||
'f' => array('ф', 'φ', 'ف', 'ƒ', 'ფ'),
|
'f' => array('ф', 'φ', 'ف', 'ƒ', 'ფ'),
|
||||||
'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ج', 'ဂ', 'გ'),
|
'g' => array('ĝ', 'ğ', 'ġ', 'ģ', 'г', 'ґ', 'γ', 'ဂ', 'გ', 'گ'),
|
||||||
'h' => array('ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ'),
|
'h' => array('ĥ', 'ħ', 'η', 'ή', 'ح', 'ه', 'ဟ', 'ှ', 'ჰ'),
|
||||||
'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į',
|
'i' => array('í', 'ì', 'ỉ', 'ĩ', 'ị', 'î', 'ï', 'ī', 'ĭ', 'į',
|
||||||
'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ',
|
'ı', 'ι', 'ί', 'ϊ', 'ΐ', 'ἰ', 'ἱ', 'ἲ', 'ἳ', 'ἴ',
|
||||||
'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ',
|
'ἵ', 'ἶ', 'ἷ', 'ὶ', 'ί', 'ῐ', 'ῑ', 'ῒ', 'ΐ', 'ῖ',
|
||||||
'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი',
|
'ῗ', 'і', 'ї', 'и', 'ဣ', 'ိ', 'ီ', 'ည်', 'ǐ', 'ი',
|
||||||
'इ'),
|
'इ', 'ی'),
|
||||||
'j' => array('ĵ', 'ј', 'Ј', 'ჯ'),
|
'j' => array('ĵ', 'ј', 'Ј', 'ჯ', 'ج'),
|
||||||
'k' => array('ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ'),
|
'k' => array('ķ', 'ĸ', 'к', 'κ', 'Ķ', 'ق', 'ك', 'က', 'კ', 'ქ',
|
||||||
|
'ک'),
|
||||||
'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ'),
|
'l' => array('ł', 'ľ', 'ĺ', 'ļ', 'ŀ', 'л', 'λ', 'ل', 'လ', 'ლ'),
|
||||||
'm' => array('м', 'μ', 'م', 'မ', 'მ'),
|
'm' => array('м', 'μ', 'م', 'မ', 'მ'),
|
||||||
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န',
|
'n' => array('ñ', 'ń', 'ň', 'ņ', 'ʼn', 'ŋ', 'ν', 'н', 'ن', 'န',
|
||||||
@@ -1634,7 +1652,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő',
|
'ộ', 'ơ', 'ớ', 'ờ', 'ở', 'ỡ', 'ợ', 'ø', 'ō', 'ő',
|
||||||
'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό',
|
'ŏ', 'ο', 'ὀ', 'ὁ', 'ὂ', 'ὃ', 'ὄ', 'ὅ', 'ὸ', 'ό',
|
||||||
'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ'),
|
'о', 'و', 'θ', 'ို', 'ǒ', 'ǿ', 'º', 'ო', 'ओ'),
|
||||||
'p' => array('п', 'π', 'ပ', 'პ'),
|
'p' => array('п', 'π', 'ပ', 'პ', 'پ'),
|
||||||
'q' => array('ყ'),
|
'q' => array('ყ'),
|
||||||
'r' => array('ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ'),
|
'r' => array('ŕ', 'ř', 'ŗ', 'р', 'ρ', 'ر', 'რ'),
|
||||||
's' => array('ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ',
|
's' => array('ś', 'š', 'ş', 'с', 'σ', 'ș', 'ς', 'س', 'ص', 'စ',
|
||||||
@@ -1650,11 +1668,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ',
|
'y' => array('ý', 'ỳ', 'ỷ', 'ỹ', 'ỵ', 'ÿ', 'ŷ', 'й', 'ы', 'υ',
|
||||||
'ϋ', 'ύ', 'ΰ', 'ي', 'ယ'),
|
'ϋ', 'ύ', 'ΰ', 'ي', 'ယ'),
|
||||||
'z' => array('ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ'),
|
'z' => array('ź', 'ž', 'ż', 'з', 'ζ', 'ز', 'ဇ', 'ზ'),
|
||||||
'aa' => array('ع', 'आ'),
|
'aa' => array('ع', 'आ', 'آ'),
|
||||||
'ae' => array('ä', 'æ', 'ǽ'),
|
'ae' => array('ä', 'æ', 'ǽ'),
|
||||||
'ai' => array('ऐ'),
|
'ai' => array('ऐ'),
|
||||||
'at' => array('@'),
|
'at' => array('@'),
|
||||||
'ch' => array('ч', 'ჩ', 'ჭ'),
|
'ch' => array('ч', 'ჩ', 'ჭ', 'چ'),
|
||||||
'dj' => array('ђ', 'đ'),
|
'dj' => array('ђ', 'đ'),
|
||||||
'dz' => array('џ', 'ძ'),
|
'dz' => array('џ', 'ძ'),
|
||||||
'ei' => array('ऍ'),
|
'ei' => array('ऍ'),
|
||||||
@@ -1664,11 +1682,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'kh' => array('х', 'خ', 'ხ'),
|
'kh' => array('х', 'خ', 'ხ'),
|
||||||
'lj' => array('љ'),
|
'lj' => array('љ'),
|
||||||
'nj' => array('њ'),
|
'nj' => array('њ'),
|
||||||
'oe' => array('ö', 'œ'),
|
'oe' => array('ö', 'œ', 'ؤ'),
|
||||||
'oi' => array('ऑ'),
|
'oi' => array('ऑ'),
|
||||||
'oii' => array('ऒ'),
|
'oii' => array('ऒ'),
|
||||||
'ps' => array('ψ'),
|
'ps' => array('ψ'),
|
||||||
'sh' => array('ш', 'შ'),
|
'sh' => array('ш', 'შ', 'ش'),
|
||||||
'shch' => array('щ'),
|
'shch' => array('щ'),
|
||||||
'ss' => array('ß'),
|
'ss' => array('ß'),
|
||||||
'sx' => array('ŝ'),
|
'sx' => array('ŝ'),
|
||||||
@@ -1678,7 +1696,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'uu' => array('ऊ'),
|
'uu' => array('ऊ'),
|
||||||
'ya' => array('я'),
|
'ya' => array('я'),
|
||||||
'yu' => array('ю'),
|
'yu' => array('ю'),
|
||||||
'zh' => array('ж', 'ჟ'),
|
'zh' => array('ж', 'ჟ', 'ژ'),
|
||||||
'(c)' => array('©'),
|
'(c)' => array('©'),
|
||||||
'A' => array('Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ',
|
'A' => array('Á', 'À', 'Ả', 'Ã', 'Ạ', 'Ă', 'Ắ', 'Ằ', 'Ẳ', 'Ẵ',
|
||||||
'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą',
|
'Ặ', 'Â', 'Ấ', 'Ầ', 'Ẩ', 'Ẫ', 'Ậ', 'Å', 'Ā', 'Ą',
|
||||||
@@ -1761,7 +1779,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
private function applyPadding($left = 0, $right = 0, $padStr = ' ')
|
private function applyPadding($left = 0, $right = 0, $padStr = ' ')
|
||||||
{
|
{
|
||||||
$stringy = static::create($this->str, $this->encoding);
|
$stringy = static::create($this->str, $this->encoding);
|
||||||
$length = mb_strlen($padStr, $stringy->encoding);
|
$length = \mb_strlen($padStr, $stringy->encoding);
|
||||||
|
|
||||||
$strLength = $stringy->length();
|
$strLength = $stringy->length();
|
||||||
$paddedLength = $strLength + $left + $right;
|
$paddedLength = $strLength + $left + $right;
|
||||||
@@ -1770,9 +1788,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
return $stringy;
|
return $stringy;
|
||||||
}
|
}
|
||||||
|
|
||||||
$leftPadding = mb_substr(str_repeat($padStr, ceil($left / $length)), 0,
|
$leftPadding = \mb_substr(str_repeat($padStr, ceil($left / $length)), 0,
|
||||||
$left, $stringy->encoding);
|
$left, $stringy->encoding);
|
||||||
$rightPadding = mb_substr(str_repeat($padStr, ceil($right / $length)),
|
$rightPadding = \mb_substr(str_repeat($padStr, ceil($right / $length)),
|
||||||
0, $right, $stringy->encoding);
|
0, $right, $stringy->encoding);
|
||||||
|
|
||||||
$stringy->str = $leftPadding . $stringy->str . $rightPadding;
|
$stringy->str = $leftPadding . $stringy->str . $rightPadding;
|
||||||
@@ -1788,12 +1806,61 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
private function matchesPattern($pattern)
|
private function matchesPattern($pattern)
|
||||||
{
|
{
|
||||||
$regexEncoding = mb_regex_encoding();
|
$regexEncoding = $this->regexEncoding();
|
||||||
mb_regex_encoding($this->encoding);
|
$this->regexEncoding($this->encoding);
|
||||||
|
|
||||||
$match = mb_ereg_match($pattern, $this->str);
|
$match = \mb_ereg_match($pattern, $this->str);
|
||||||
mb_regex_encoding($regexEncoding);
|
$this->regexEncoding($regexEncoding);
|
||||||
|
|
||||||
return $match;
|
return $match;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alias for mb_ereg_replace with a fallback to preg_replace if the
|
||||||
|
* mbstring module is not installed.
|
||||||
|
*/
|
||||||
|
private function eregReplace($pattern, $replacement, $string, $option = 'msr')
|
||||||
|
{
|
||||||
|
static $functionExists;
|
||||||
|
if ($functionExists === null) {
|
||||||
|
$functionExists = function_exists('\mb_split');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($functionExists) {
|
||||||
|
return \mb_ereg_replace($pattern, $replacement, $string, $option);
|
||||||
|
} else if ($this->supportsEncoding()) {
|
||||||
|
$option = str_replace('r', '', $option);
|
||||||
|
return \preg_replace("/$pattern/u$option", $replacement, $string);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Alias for mb_regex_encoding which default to a noop if the mbstring
|
||||||
|
* module is not installed.
|
||||||
|
*/
|
||||||
|
private function regexEncoding()
|
||||||
|
{
|
||||||
|
static $functionExists;
|
||||||
|
|
||||||
|
if ($functionExists === null) {
|
||||||
|
$functionExists = function_exists('\mb_regex_encoding');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($functionExists) {
|
||||||
|
$args = func_get_args();
|
||||||
|
return call_user_func_array('\mb_regex_encoding', $args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function supportsEncoding()
|
||||||
|
{
|
||||||
|
$supported = array('UTF-8' => true, 'ASCII' => true);
|
||||||
|
|
||||||
|
if (isset($supported[$this->encoding])) {
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
throw new \RuntimeExpception('Stringy method requires the ' .
|
||||||
|
'mbstring module for encodings other than ASCII and UTF-8');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,7 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . '/../src/Create.php';
|
|
||||||
|
|
||||||
use function Stringy\create as s;
|
use function Stringy\create as s;
|
||||||
|
|
||||||
class CreateTestCase extends PHPUnit_Framework_TestCase
|
class CreateTestCase extends PHPUnit_Framework_TestCase
|
||||||
|
@@ -1,7 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . '/../src/StaticStringy.php';
|
|
||||||
|
|
||||||
use Stringy\StaticStringy as S;
|
use Stringy\StaticStringy as S;
|
||||||
|
|
||||||
class StaticStringyTestCase extends PHPUnit_Framework_TestCase
|
class StaticStringyTestCase extends PHPUnit_Framework_TestCase
|
||||||
|
@@ -1,7 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require __DIR__ . '/../src/Stringy.php';
|
|
||||||
|
|
||||||
use Stringy\Stringy as S;
|
use Stringy\Stringy as S;
|
||||||
|
|
||||||
class StringyTestCase extends PHPUnit_Framework_TestCase
|
class StringyTestCase extends PHPUnit_Framework_TestCase
|
||||||
@@ -2374,6 +2372,7 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
|
|||||||
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
|
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
|
||||||
array('', '', '', '', 'msr', 'UTF-8'),
|
array('', '', '', '', 'msr', 'UTF-8'),
|
||||||
array('bàř', 'fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'),
|
array('bàř', 'fòô ', 'f[òô]+\s', 'bàř', 'msr', 'UTF-8'),
|
||||||
|
array('fòô', 'fò', '(ò)', '\\1ô', 'msr', 'UTF-8'),
|
||||||
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
|
array('fòô', 'bàř', '[[:alpha:]]{3}', 'fòô', 'msr', 'UTF-8')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user