mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-01 09:03:03 +02:00
Compare commits
30 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
63ad4d238f | ||
|
ece219b609 | ||
|
e81dbaa6d9 | ||
|
2b2c03bd1f | ||
|
7600549abc | ||
|
8fe30d18b7 | ||
|
ae7527ad4a | ||
|
f74f535b78 | ||
|
5c5087db13 | ||
|
8d961e25d0 | ||
|
e46797a9f8 | ||
|
f0805f522d | ||
|
6103eca714 | ||
|
952ffd4c00 | ||
|
7832e22e09 | ||
|
bcd87c69b8 | ||
|
1d9001bcf0 | ||
|
ee88cd93e8 | ||
|
48f53e98fa | ||
|
6e7415a9f5 | ||
|
c62a5e8d4e | ||
|
cd41723b12 | ||
|
404909ffaa | ||
|
210c928b79 | ||
|
1965da2e39 | ||
|
25fa7340ed | ||
|
e5f66c6eca | ||
|
5d7e9a8d36 | ||
|
a7c5b8721c | ||
|
9399f7a694 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
|||||||
vendor/
|
vendor/
|
||||||
|
composer.lock
|
||||||
|
.DS_Store
|
||||||
|
@@ -1,5 +1,10 @@
|
|||||||
language: php
|
language: php
|
||||||
php:
|
php:
|
||||||
|
- 5.6
|
||||||
- 5.5
|
- 5.5
|
||||||
- 5.4
|
- 5.4
|
||||||
- 5.3
|
- 5.3
|
||||||
|
- hhvm
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
fast_finish: true
|
||||||
|
20
CHANGELOG.md
20
CHANGELOG.md
@@ -1,3 +1,23 @@
|
|||||||
|
### 1.5.2 (2014-07-09)
|
||||||
|
|
||||||
|
* Announced support for HHVM
|
||||||
|
|
||||||
|
### 1.5.1 (2014-04-19)
|
||||||
|
|
||||||
|
* 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
|
||||||
|
|
||||||
|
### 1.5.0 (2014-03-19)
|
||||||
|
|
||||||
|
* 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()
|
||||||
|
|
||||||
### 1.4.0 (2014-02-12)
|
### 1.4.0 (2014-02-12)
|
||||||
|
|
||||||
* Implemented the IteratorAggregate interface, added chars()
|
* Implemented the IteratorAggregate interface, added chars()
|
||||||
|
23
README.md
23
README.md
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
A PHP library with a variety of string manipulation functions with multibyte
|
A PHP library with a variety of string manipulation functions with multibyte
|
||||||
support. Offers both OO method chaining and a procedural-style static wrapper.
|
support. Offers both OO method chaining and a procedural-style static wrapper.
|
||||||
Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
Tested and compatible with PHP 5.3+ and HHVM. Inspired by underscore.string.js.
|
||||||
|
|
||||||
[](https://travis-ci.org/danielstjules/Stringy)
|
[](https://travis-ci.org/danielstjules/Stringy)
|
||||||
|
|
||||||
@@ -22,6 +22,7 @@ Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
|||||||
* [ensureLeft](#ensureleft)
|
* [ensureLeft](#ensureleft)
|
||||||
* [ensureRight](#ensureright)
|
* [ensureRight](#ensureright)
|
||||||
* [first](#first)
|
* [first](#first)
|
||||||
|
* [getEncoding](#getencoding)
|
||||||
* [humanize](#humanize)
|
* [humanize](#humanize)
|
||||||
* [insert](#insert)
|
* [insert](#insert)
|
||||||
* [isAlpha](#isalpha)
|
* [isAlpha](#isalpha)
|
||||||
@@ -66,6 +67,7 @@ Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
|||||||
* [underscored](#underscored)
|
* [underscored](#underscored)
|
||||||
* [upperCamelize](#uppercamelize)
|
* [upperCamelize](#uppercamelize)
|
||||||
* [upperCaseFirst](#uppercasefirst)
|
* [upperCaseFirst](#uppercasefirst)
|
||||||
|
* [Links](#links)
|
||||||
* [Tests](#tests)
|
* [Tests](#tests)
|
||||||
* [License](#license)
|
* [License](#license)
|
||||||
|
|
||||||
@@ -75,7 +77,7 @@ If you're using Composer to manage dependencies, you can include the following
|
|||||||
in your composer.json file:
|
in your composer.json file:
|
||||||
|
|
||||||
"require": {
|
"require": {
|
||||||
"danielstjules/stringy": "dev-master"
|
"danielstjules/stringy": ">=1.5.2"
|
||||||
}
|
}
|
||||||
|
|
||||||
Then, after running `composer update` or `php composer.phar update`, you can
|
Then, after running `composer update` or `php composer.phar update`, you can
|
||||||
@@ -340,6 +342,16 @@ S::create('fòô bàř', 'UTF-8')->first(3);
|
|||||||
S::first('fòô bàř', 3, 'UTF-8'); // 'fòô'
|
S::first('fòô bàř', 3, 'UTF-8'); // 'fòô'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### getEncoding
|
||||||
|
|
||||||
|
$stringy->getEncoding()
|
||||||
|
|
||||||
|
Returns the encoding used by the Stringy object.
|
||||||
|
|
||||||
|
```php
|
||||||
|
S::create('fòô bàř', 'UTF-8')->getEncoding(); // 'UTF-8'
|
||||||
|
```
|
||||||
|
|
||||||
#### humanize
|
#### humanize
|
||||||
|
|
||||||
$stringy->humanize()
|
$stringy->humanize()
|
||||||
@@ -954,6 +966,13 @@ S::create('σ test', 'UTF-8')->upperCaseFirst();
|
|||||||
S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Links
|
||||||
|
|
||||||
|
The following is a list of libraries that extend Stringy:
|
||||||
|
|
||||||
|
* [SliceableStringy](https://github.com/danielstjules/SliceableStringy):
|
||||||
|
Python-like string slices in PHP
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
From the project directory, tests can be ran using `phpunit`
|
From the project directory, tests can be ran using `phpunit`
|
||||||
|
@@ -17,11 +17,15 @@
|
|||||||
"require": {
|
"require": {
|
||||||
"php": ">=5.3.0"
|
"php": ">=5.3.0"
|
||||||
},
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "4.0.*"
|
||||||
|
},
|
||||||
"support": {
|
"support": {
|
||||||
"issues": "https://github.com/danielstjules/Stringy/issues",
|
"issues": "https://github.com/danielstjules/Stringy/issues",
|
||||||
"source": "https://github.com/danielstjules/Stringy"
|
"source": "https://github.com/danielstjules/Stringy"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": { "Stringy\\": "src/" }
|
"psr-4": { "Stringy\\": "src/" },
|
||||||
|
"classmap": [ "tests" ]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
173
src/Stringy.php
173
src/Stringy.php
@@ -4,9 +4,9 @@ namespace Stringy;
|
|||||||
|
|
||||||
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||||
{
|
{
|
||||||
private $str;
|
protected $str;
|
||||||
|
|
||||||
public $encoding;
|
protected $encoding;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes a Stringy object and assigns both str and encoding properties
|
* Initializes a Stringy object and assigns both str and encoding properties
|
||||||
@@ -64,6 +64,16 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
return $this->str;
|
return $this->str;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the encoding used by the Stringy object.
|
||||||
|
*
|
||||||
|
* @return string The current value of the $encoding property
|
||||||
|
*/
|
||||||
|
public function getEncoding()
|
||||||
|
{
|
||||||
|
return $this->encoding;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the length of the string, implementing the countable interface.
|
* Returns the length of the string, implementing the countable interface.
|
||||||
*
|
*
|
||||||
@@ -252,17 +262,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function dasherize()
|
public function dasherize()
|
||||||
{
|
{
|
||||||
// Save current regex encoding so we can reset it after
|
return $this->applyDelimeter('-');
|
||||||
$regexEncoding = mb_regex_encoding();
|
|
||||||
mb_regex_encoding($this->encoding);
|
|
||||||
|
|
||||||
$str = mb_ereg_replace('\B([A-Z])', '-\1', trim($this->str));
|
|
||||||
$str = mb_ereg_replace('[-_\s]+', '-', $str);
|
|
||||||
$str = mb_strtolower($str, $this->encoding);
|
|
||||||
|
|
||||||
mb_regex_encoding($regexEncoding);
|
|
||||||
|
|
||||||
return self::create($str, $this->encoding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -274,13 +274,24 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
* @return Stringy Object with an underscored $str
|
* @return Stringy Object with an underscored $str
|
||||||
*/
|
*/
|
||||||
public function underscored()
|
public function underscored()
|
||||||
|
{
|
||||||
|
return $this->applyDelimeter('_');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a lowercase and trimmed string separated by the given delimiter.
|
||||||
|
*
|
||||||
|
* @param string $delimiter Sequence used to separate parts of the string
|
||||||
|
* @return Stringy Object with a delimited $str
|
||||||
|
*/
|
||||||
|
protected function applyDelimeter($delimiter)
|
||||||
{
|
{
|
||||||
// Save current regex encoding so we can reset it after
|
// Save current regex encoding so we can reset it after
|
||||||
$regexEncoding = mb_regex_encoding();
|
$regexEncoding = mb_regex_encoding();
|
||||||
mb_regex_encoding($this->encoding);
|
mb_regex_encoding($this->encoding);
|
||||||
|
|
||||||
$str = mb_ereg_replace('\B([A-Z])', '_\1', trim($this->str));
|
$str = mb_ereg_replace('\B([A-Z])', $delimiter .'\1', trim($this->str));
|
||||||
$str = mb_ereg_replace('[-_\s]+', '_', $str);
|
$str = mb_ereg_replace('[-_\s]+', $delimiter, $str);
|
||||||
$str = mb_strtolower($str, $this->encoding);
|
$str = mb_strtolower($str, $this->encoding);
|
||||||
|
|
||||||
mb_regex_encoding($regexEncoding);
|
mb_regex_encoding($regexEncoding);
|
||||||
@@ -383,13 +394,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function collapseWhitespace()
|
public function collapseWhitespace()
|
||||||
{
|
{
|
||||||
$regexEncoding = mb_regex_encoding();
|
return $this->regexReplace('[[:space:]]+', ' ')->trim();
|
||||||
mb_regex_encoding($this->encoding);
|
|
||||||
|
|
||||||
$str = mb_ereg_replace('[[:space:]]+', ' ', $this->str);
|
|
||||||
mb_regex_encoding($regexEncoding);
|
|
||||||
|
|
||||||
return self::create($str, $this->encoding)->trim();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -405,7 +410,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
|
'a' => array('à', 'á', 'â', 'ã', 'ā', 'ą', 'ă', 'å', 'α', 'ά', 'ἀ',
|
||||||
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
|
'ἁ', 'ἂ', 'ἃ', 'ἄ', 'ἅ', 'ἆ', 'ἇ', 'ᾀ', 'ᾁ', 'ᾂ', 'ᾃ',
|
||||||
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ',
|
'ᾄ', 'ᾅ', 'ᾆ', 'ᾇ', 'ὰ', 'ά', 'ᾰ', 'ᾱ', 'ᾲ', 'ᾳ', 'ᾴ',
|
||||||
'ᾶ', 'ᾷ', 'а', 'ъ'),
|
'ᾶ', 'ᾷ', 'а'),
|
||||||
'b' => array('б', 'β'),
|
'b' => array('б', 'β'),
|
||||||
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
|
'c' => array('ç', 'ć', 'č', 'ĉ', 'ċ'),
|
||||||
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д'),
|
'd' => array('ď', 'ð', 'đ', 'ƌ', 'ȡ', 'ɖ', 'ɗ', 'ᵭ', 'ᶁ', 'ᶑ', 'д'),
|
||||||
@@ -444,7 +449,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
'zh' => array('ж'),
|
'zh' => array('ж'),
|
||||||
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ',
|
'A' => array('Á', 'Â', 'Ã', 'Å', 'Ā', 'Ą', 'Ă', 'Α', 'Ά', 'Ἀ', 'Ἁ',
|
||||||
'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ',
|
'Ἂ', 'Ἃ', 'Ἄ', 'Ἅ', 'Ἆ', 'Ἇ', 'ᾈ', 'ᾉ', 'ᾊ', 'ᾋ', 'ᾌ',
|
||||||
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А', 'Ъ'),
|
'ᾍ', 'ᾎ', 'ᾏ', 'Ᾰ', 'Ᾱ', 'Ὰ', 'Ά', 'ᾼ', 'А'),
|
||||||
'B' => array('Б'),
|
'B' => array('Б'),
|
||||||
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
|
'C' => array('Ć', 'Č', 'Ĉ', 'Ċ'),
|
||||||
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д'),
|
'D' => array('Ď', 'Ð', 'Đ', 'Ɖ', 'Ɗ', 'Ƌ', 'ᴅ', 'ᴆ', 'Д'),
|
||||||
@@ -483,6 +488,8 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
$str = str_replace($value, $key, $str);
|
$str = str_replace($value, $key, $str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$str = preg_replace('/[^\x20-\x7E]/u', '', $str);
|
||||||
|
|
||||||
return self::create($str, $this->encoding);
|
return self::create($str, $this->encoding);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -507,44 +514,14 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
"to be one of 'left', 'right' or 'both'");
|
"to be one of 'left', 'right' or 'both'");
|
||||||
}
|
}
|
||||||
|
|
||||||
$stringy = self::create($this->str, $this->encoding);
|
switch ($padType) {
|
||||||
$encoding = $stringy->encoding;
|
case 'left':
|
||||||
$strLength = $stringy->length();
|
return $this->padLeft($length, $padStr);
|
||||||
$padStrLength = mb_strlen($padStr, $encoding);
|
case 'right':
|
||||||
|
return $this->padRight($length, $padStr);
|
||||||
if ($length <= $strLength || $padStrLength <= 0) {
|
default:
|
||||||
return $stringy;
|
return $this->padBoth($length, $padStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Number of times to repeat the padStr if left or right
|
|
||||||
$times = ceil(($length - $strLength) / $padStrLength);
|
|
||||||
$paddedStr = '';
|
|
||||||
|
|
||||||
if ($padType == 'left') {
|
|
||||||
// Repeat the pad, cut it, and prepend
|
|
||||||
$leftPad = str_repeat($padStr, $times);
|
|
||||||
$leftPad = mb_substr($leftPad, 0, $length - $strLength, $encoding);
|
|
||||||
$stringy->str = $leftPad . $stringy->str;
|
|
||||||
} elseif ($padType == 'right') {
|
|
||||||
// Append the repeated pad and get a substring of the given length
|
|
||||||
$stringy->str = $stringy->str . str_repeat($padStr, $times);
|
|
||||||
$stringy->str = mb_substr($stringy->str, 0, $length, $encoding);
|
|
||||||
} else {
|
|
||||||
// Number of times to repeat the padStr on both sides
|
|
||||||
$paddingSize = ($length - $strLength) / 2;
|
|
||||||
$times = ceil($paddingSize / $padStrLength);
|
|
||||||
|
|
||||||
// Favour right padding over left, as with str_pad()
|
|
||||||
$rightPad = str_repeat($padStr, $times);
|
|
||||||
$rightPad = mb_substr($rightPad, 0, ceil($paddingSize), $encoding);
|
|
||||||
|
|
||||||
$leftPad = str_repeat($padStr, $times);
|
|
||||||
$leftPad = mb_substr($leftPad, 0, floor($paddingSize), $encoding);
|
|
||||||
|
|
||||||
$stringy->str = $leftPad . $stringy->str . $rightPad;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $stringy;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -553,11 +530,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*
|
*
|
||||||
* @param int $length Desired string length after padding
|
* @param int $length Desired string length after padding
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param string $padStr String used to pad, defaults to space
|
||||||
* @return Stringy Object with a left padded $str
|
* @return Stringy String with left padding
|
||||||
*/
|
*/
|
||||||
public function padLeft($length, $padStr = ' ')
|
public function padLeft($length, $padStr = ' ')
|
||||||
{
|
{
|
||||||
return $this->pad($length, $padStr, 'left');
|
return $this->applyPadding($length - $this->length(), 0, $padStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -566,11 +543,11 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*
|
*
|
||||||
* @param int $length Desired string length after padding
|
* @param int $length Desired string length after padding
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param string $padStr String used to pad, defaults to space
|
||||||
* @return Stringy Object with a right padded $str
|
* @return Stringy String with right padding
|
||||||
*/
|
*/
|
||||||
public function padRight($length, $padStr = ' ')
|
public function padRight($length, $padStr = ' ')
|
||||||
{
|
{
|
||||||
return $this->pad($length, $padStr, 'right');
|
return $this->applyPadding(0, $length - $this->length(), $padStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -579,11 +556,45 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*
|
*
|
||||||
* @param int $length Desired string length after padding
|
* @param int $length Desired string length after padding
|
||||||
* @param string $padStr String used to pad, defaults to space
|
* @param string $padStr String used to pad, defaults to space
|
||||||
* @return Stringy The padded string
|
* @return Stringy String with padding applied
|
||||||
*/
|
*/
|
||||||
public function padBoth($length, $padStr = ' ')
|
public function padBoth($length, $padStr = ' ')
|
||||||
{
|
{
|
||||||
return $this->pad($length, $padStr, 'both');
|
$padding = $length - $this->length();
|
||||||
|
|
||||||
|
return $this->applyPadding(floor($padding / 2), ceil($padding / 2),
|
||||||
|
$padStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the specified amount of left and right padding to the given string.
|
||||||
|
* The default character used is a space.
|
||||||
|
*
|
||||||
|
* @param int $left Length of left padding
|
||||||
|
* @param int $right Length of right padding
|
||||||
|
* @param string $padStr String used to pad
|
||||||
|
* @return Stringy String with padding applied
|
||||||
|
*/
|
||||||
|
private function applyPadding($left = 0, $right = 0, $padStr = ' ')
|
||||||
|
{
|
||||||
|
$stringy = self::create($this->str, $this->encoding);
|
||||||
|
$length = mb_strlen($padStr, $stringy->encoding);
|
||||||
|
|
||||||
|
$strLength = $stringy->length();
|
||||||
|
$paddedLength = $strLength + $left + $right;
|
||||||
|
|
||||||
|
if (!$length || $paddedLength <= $strLength) {
|
||||||
|
return $stringy;
|
||||||
|
}
|
||||||
|
|
||||||
|
$leftPadding = mb_substr(str_repeat($padStr, ceil($left / $length)), 0,
|
||||||
|
$left, $stringy->encoding);
|
||||||
|
$rightPadding = mb_substr(str_repeat($padStr, ceil($right / $length)), 0,
|
||||||
|
$right, $stringy->encoding);
|
||||||
|
|
||||||
|
$stringy->str = $leftPadding . $stringy->str . $rightPadding;
|
||||||
|
|
||||||
|
return $stringy;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -705,12 +716,12 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
{
|
{
|
||||||
$stringy = self::create($this->str, $this->encoding);
|
$stringy = self::create($this->str, $this->encoding);
|
||||||
|
|
||||||
$stringy->str = preg_replace("/[^a-zA-Z\d $replacement]/u", '',
|
$quotedReplacement = preg_quote($replacement);
|
||||||
$stringy->toAscii());
|
$pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
|
||||||
$stringy->str = $stringy->collapseWhitespace()->str;
|
$stringy->str = preg_replace($pattern, '', $stringy->toAscii());
|
||||||
$stringy->str = str_replace(' ', $replacement, strtolower($stringy));
|
|
||||||
|
|
||||||
return $stringy;
|
return $stringy->toLowerCase()->applyDelimeter($replacement)
|
||||||
|
->removeLeft($replacement)->removeRight($replacement);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1199,11 +1210,9 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function isJson()
|
public function isJson()
|
||||||
{
|
{
|
||||||
if (!$this->endsWith('}') && !$this->endsWith(']')) {
|
json_decode($this->str);
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return !is_null(json_decode($this->str));
|
return (json_last_error() === JSON_ERROR_NONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1269,17 +1278,7 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
|||||||
*/
|
*/
|
||||||
public function replace($search, $replacement)
|
public function replace($search, $replacement)
|
||||||
{
|
{
|
||||||
$regexEncoding = mb_regex_encoding();
|
return $this->regexReplace(preg_quote($search), $replacement);
|
||||||
mb_regex_encoding($this->encoding);
|
|
||||||
|
|
||||||
// Don't want the args being parsed as regex
|
|
||||||
$search = preg_quote($search);
|
|
||||||
$replacement = preg_quote($replacement);
|
|
||||||
|
|
||||||
$str = mb_ereg_replace($search, $replacement, $this->str);
|
|
||||||
mb_regex_encoding($regexEncoding);
|
|
||||||
|
|
||||||
return self::create($str, $this->encoding);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -174,17 +174,18 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return array(
|
return array(
|
||||||
array('foo bar', 'fòô bàř'),
|
array('foo bar', 'fòô bàř'),
|
||||||
array(' TEST ', ' ŤÉŚŢ '),
|
array(' TEST ', ' ŤÉŚŢ '),
|
||||||
array('φ = z = 3', 'φ = ź = 3'),
|
array(' = z = 3', 'φ = ź = 3'),
|
||||||
array('perevirka', 'перевірка'),
|
array('perevirka', 'перевірка'),
|
||||||
array('lysaya gora', 'лысая гора'),
|
array('lysaya gora', 'лысая гора'),
|
||||||
array('shchuka', 'щука')
|
array('shchuka', 'щука'),
|
||||||
|
array('', '漢字')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function padProvider()
|
public function padProvider()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
// $length <= $str
|
// length <= str
|
||||||
array('foo bar', 'foo bar', -1),
|
array('foo bar', 'foo bar', -1),
|
||||||
array('foo bar', 'foo bar', 7),
|
array('foo bar', 'foo bar', 7),
|
||||||
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
|
array('fòô bàř', 'fòô bàř', 7, ' ', 'right', 'UTF-8'),
|
||||||
@@ -192,32 +193,16 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
// right
|
// right
|
||||||
array('foo bar ', 'foo bar', 9),
|
array('foo bar ', 'foo bar', 9),
|
||||||
array('foo bar_*', 'foo bar', 9, '_*', 'right'),
|
array('foo bar_*', 'foo bar', 9, '_*', 'right'),
|
||||||
array('foo bar_*_', 'foo bar', 10, '_*', 'right'),
|
|
||||||
array('fòô bàř ', 'fòô bàř', 9, ' ', 'right', 'UTF-8'),
|
|
||||||
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'right', 'UTF-8'),
|
|
||||||
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'),
|
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'right', 'UTF-8'),
|
||||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'right', 'UTF-8'),
|
|
||||||
|
|
||||||
// left
|
// left
|
||||||
array(' foo bar', 'foo bar', 9, ' ', 'left'),
|
array(' foo bar', 'foo bar', 9, ' ', 'left'),
|
||||||
array('_*foo bar', 'foo bar', 9, '_*', 'left'),
|
array('_*foo bar', 'foo bar', 9, '_*', 'left'),
|
||||||
array('_*_foo bar', 'foo bar', 10, '_*', 'left'),
|
|
||||||
array(' fòô bàř', 'fòô bàř', 9, ' ', 'left', 'UTF-8'),
|
|
||||||
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'left', 'UTF-8'),
|
|
||||||
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
|
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'left', 'UTF-8'),
|
||||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'left', 'UTF-8'),
|
|
||||||
|
|
||||||
// both
|
// both
|
||||||
array('foo bar ', 'foo bar', 8, ' ', 'both'),
|
array('foo bar ', 'foo bar', 8, ' ', 'both'),
|
||||||
array(' foo bar ', 'foo bar', 9, ' ', 'both'),
|
|
||||||
array('fòô bàř ', 'fòô bàř', 8, ' ', 'both', 'UTF-8'),
|
|
||||||
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'both', 'UTF-8'),
|
|
||||||
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'both', 'UTF-8'),
|
|
||||||
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'both', 'UTF-8'),
|
|
||||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'),
|
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'both', 'UTF-8'),
|
||||||
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'both', 'UTF-8'),
|
|
||||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'both', 'UTF-8'),
|
|
||||||
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'both', 'UTF-8'),
|
|
||||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
|
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'both', 'UTF-8')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -226,7 +211,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(' foo bar', 'foo bar', 9),
|
array(' foo bar', 'foo bar', 9),
|
||||||
|
array('_*foo bar', 'foo bar', 9, '_*'),
|
||||||
array('_*_foo bar', 'foo bar', 10, '_*'),
|
array('_*_foo bar', 'foo bar', 10, '_*'),
|
||||||
|
array(' fòô bàř', 'fòô bàř', 9, ' ', 'UTF-8'),
|
||||||
|
array('¬øfòô bàř', 'fòô bàř', 9, '¬ø', 'UTF-8'),
|
||||||
|
array('¬ø¬fòô bàř', 'fòô bàř', 10, '¬ø', 'UTF-8'),
|
||||||
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
array('¬ø¬øfòô bàř', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -235,7 +224,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('foo bar ', 'foo bar', 9),
|
array('foo bar ', 'foo bar', 9),
|
||||||
|
array('foo bar_*', 'foo bar', 9, '_*'),
|
||||||
array('foo bar_*_', 'foo bar', 10, '_*'),
|
array('foo bar_*_', 'foo bar', 10, '_*'),
|
||||||
|
array('fòô bàř ', 'fòô bàř', 9, ' ', 'UTF-8'),
|
||||||
|
array('fòô bàř¬ø', 'fòô bàř', 9, '¬ø', 'UTF-8'),
|
||||||
|
array('fòô bàř¬ø¬', 'fòô bàř', 10, '¬ø', 'UTF-8'),
|
||||||
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
array('fòô bàř¬ø¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -245,7 +238,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return array(
|
return array(
|
||||||
array('foo bar ', 'foo bar', 8),
|
array('foo bar ', 'foo bar', 8),
|
||||||
array(' foo bar ', 'foo bar', 9, ' '),
|
array(' foo bar ', 'foo bar', 9, ' '),
|
||||||
|
array('fòô bàř ', 'fòô bàř', 8, ' ', 'UTF-8'),
|
||||||
|
array(' fòô bàř ', 'fòô bàř', 9, ' ', 'UTF-8'),
|
||||||
|
array('fòô bàř¬', 'fòô bàř', 8, '¬ø', 'UTF-8'),
|
||||||
|
array('¬fòô bàř¬', 'fòô bàř', 9, '¬ø', 'UTF-8'),
|
||||||
|
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬ø', 'UTF-8'),
|
||||||
|
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬ø', 'UTF-8'),
|
||||||
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
|
array('¬fòô bàř¬ø', 'fòô bàř', 10, '¬øÿ', 'UTF-8'),
|
||||||
|
array('¬øfòô bàř¬ø', 'fòô bàř', 11, '¬øÿ', 'UTF-8'),
|
||||||
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
|
array('¬øfòô bàř¬øÿ', 'fòô bàř', 12, '¬øÿ', 'UTF-8')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -333,6 +333,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array('foo-bar', ' foo bar '),
|
array('foo-bar', ' foo bar '),
|
||||||
|
array('foo-bar', 'foo -.-"-...bar'),
|
||||||
|
array('another-foo-bar', 'another..& foo -.-"-...bar'),
|
||||||
array('foo-dbar', " Foo d'Bar "),
|
array('foo-dbar', " Foo d'Bar "),
|
||||||
array('a-string-with-dashes', 'A string-with-dashes'),
|
array('a-string-with-dashes', 'A string-with-dashes'),
|
||||||
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
|
array('using-strings-like-foo-bar', 'Using strings like fòô bàř'),
|
||||||
@@ -340,9 +342,12 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
array('numbers-1234', 'numbers 1234'),
|
array('numbers-1234', 'numbers 1234'),
|
||||||
array('perevirka-ryadka', 'перевірка рядка'),
|
array('perevirka-ryadka', 'перевірка рядка'),
|
||||||
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
|
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
|
||||||
array('barzi-i-yarostni', 'бързи и яростни'),
|
array('podekhal-k-podezdu-moego-doma', 'подъехал к подъезду моего дома'),
|
||||||
array('foo:bar:baz', 'Foo bar baz', ':'),
|
array('foo:bar:baz', 'Foo bar baz', ':'),
|
||||||
array('a_string_with_underscores', 'A_string with_underscores', '_')
|
array('a_string_with_underscores', 'A_string with_underscores', '_'),
|
||||||
|
array('a_string_with_dashes', 'A string-with-dashes', '_'),
|
||||||
|
array('a\string\with\dashes', 'A string-with-dashes', '\\'),
|
||||||
|
array('an_odd_string', '-- An odd__ string-_', '_')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -730,14 +735,14 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
public function isJsonProvider()
|
public function isJsonProvider()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
array(false, ''),
|
array(true, ''),
|
||||||
array(false, '123'),
|
array(true, '123'),
|
||||||
array(true, '{"foo": "bar"}'),
|
array(true, '{"foo": "bar"}'),
|
||||||
array(false, '{"foo":"bar",}'),
|
array(false, '{"foo":"bar",}'),
|
||||||
array(false, '{"foo"}'),
|
array(false, '{"foo"}'),
|
||||||
array(true, '["foo"]'),
|
array(true, '["foo"]'),
|
||||||
array(false, '{"foo": "bar"]'),
|
array(false, '{"foo": "bar"]'),
|
||||||
array(false, '123', 'UTF-8'),
|
array(true, '123', 'UTF-8'),
|
||||||
array(true, '{"fòô": "bàř"}', 'UTF-8'),
|
array(true, '{"fòô": "bàř"}', 'UTF-8'),
|
||||||
array(false, '{"fòô":"bàř",}', 'UTF-8'),
|
array(false, '{"fòô":"bàř",}', 'UTF-8'),
|
||||||
array(false, '{"fòô"}', 'UTF-8'),
|
array(false, '{"fòô"}', 'UTF-8'),
|
||||||
@@ -835,6 +840,8 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
array('foo', '', '', 'foo'),
|
array('foo', '', '', 'foo'),
|
||||||
array('foo', '\s', '\s', 'foo'),
|
array('foo', '\s', '\s', 'foo'),
|
||||||
array('foo bar', 'foo bar', '', ''),
|
array('foo bar', 'foo bar', '', ''),
|
||||||
|
array('foo bar', 'foo bar', 'f(o)o', '\1'),
|
||||||
|
array('\1 bar', 'foo bar', 'foo', '\1'),
|
||||||
array('bar', 'foo bar', 'foo ', ''),
|
array('bar', 'foo bar', 'foo ', ''),
|
||||||
array('far bar', 'foo bar', 'foo', 'far'),
|
array('far bar', 'foo bar', 'foo', 'far'),
|
||||||
array('bar bar', 'foo bar foo bar', 'foo ', ''),
|
array('bar bar', 'foo bar foo bar', 'foo ', ''),
|
||||||
@@ -853,6 +860,7 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
|||||||
return array(
|
return array(
|
||||||
array('', '', '', ''),
|
array('', '', '', ''),
|
||||||
array('bar', 'foo', 'f[o]+', 'bar'),
|
array('bar', 'foo', 'f[o]+', 'bar'),
|
||||||
|
array('o bar', 'foo bar', 'f(o)o', '\1'),
|
||||||
array('bar', 'foo bar', 'f[O]+\s', '', 'i'),
|
array('bar', 'foo bar', 'f[O]+\s', '', 'i'),
|
||||||
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
|
array('foo', 'bar', '[[:alpha:]]{3}', 'foo'),
|
||||||
array('', '', '', '', 'msr', 'UTF-8'),
|
array('', '', '', '', 'msr', 'UTF-8'),
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$base = realpath(dirname(__FILE__) . '/..');
|
require __DIR__ . '/../src/StaticStringy.php';
|
||||||
require("$base/src/StaticStringy.php");
|
|
||||||
|
|
||||||
use Stringy\StaticStringy as S;
|
use Stringy\StaticStringy as S;
|
||||||
|
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
$base = realpath(dirname(__FILE__) . '/..');
|
require __DIR__ . '/../src/Stringy.php';
|
||||||
require("$base/src/Stringy.php");
|
|
||||||
|
|
||||||
use Stringy\Stringy as S;
|
use Stringy\Stringy as S;
|
||||||
|
|
||||||
@@ -12,7 +11,7 @@ class StringyTestCase extends CommonTest
|
|||||||
$stringy = new S('foo bar', 'UTF-8');
|
$stringy = new S('foo bar', 'UTF-8');
|
||||||
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
||||||
$this->assertEquals('foo bar', (string) $stringy);
|
$this->assertEquals('foo bar', (string) $stringy);
|
||||||
$this->assertEquals('UTF-8', $stringy->encoding);
|
$this->assertEquals('UTF-8', $stringy->getEncoding());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -59,7 +58,7 @@ class StringyTestCase extends CommonTest
|
|||||||
$stringy = S::create('foo bar', 'UTF-8');
|
$stringy = S::create('foo bar', 'UTF-8');
|
||||||
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
$this->assertInstanceOf('Stringy\Stringy', $stringy);
|
||||||
$this->assertEquals('foo bar', (string) $stringy);
|
$this->assertEquals('foo bar', (string) $stringy);
|
||||||
$this->assertEquals('UTF-8', $stringy->encoding);
|
$this->assertEquals('UTF-8', $stringy->getEncoding());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testChaining()
|
public function testChaining()
|
||||||
|
Reference in New Issue
Block a user