mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-09-01 09:03:03 +02:00
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
7e91d1ead2 | ||
|
abf3e38c1c | ||
|
5a5c39d02b | ||
|
c577684e0a | ||
|
681463de87 | ||
|
90de5cca00 | ||
|
a0b7615210 | ||
|
63ad4d238f | ||
|
ece219b609 | ||
|
e81dbaa6d9 | ||
|
2b2c03bd1f | ||
|
7600549abc | ||
|
8fe30d18b7 | ||
|
ae7527ad4a | ||
|
f74f535b78 | ||
|
5c5087db13 |
@@ -1,5 +1,7 @@
|
||||
language: php
|
||||
php:
|
||||
- 5.6
|
||||
- 5.5
|
||||
- 5.4
|
||||
- 5.3
|
||||
- hhvm
|
||||
|
14
CHANGELOG.md
14
CHANGELOG.md
@@ -1,3 +1,17 @@
|
||||
### 1.6.0 (2014-09-14)
|
||||
|
||||
* Added toTitleCase
|
||||
|
||||
### 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
|
||||
|
20
README.md
20
README.md
@@ -1,8 +1,8 @@
|
||||

|
||||

|
||||
|
||||
A PHP library with a variety of string manipulation functions with multibyte
|
||||
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)
|
||||
|
||||
@@ -61,6 +61,7 @@ Compatible with PHP 5.3+. Inspired by underscore.string.js.
|
||||
* [toLowerCase](#tolowercase)
|
||||
* [toSpaces](#tospaces)
|
||||
* [toTabs](#totabs)
|
||||
* [toTitleCase](#totitlecase)
|
||||
* [toUpperCase](#touppercase)
|
||||
* [trim](#trim)
|
||||
* [truncate](#truncate)
|
||||
@@ -77,7 +78,7 @@ If you're using Composer to manage dependencies, you can include the following
|
||||
in your composer.json file:
|
||||
|
||||
"require": {
|
||||
"danielstjules/stringy": ">=1.5.0"
|
||||
"danielstjules/stringy": ">=1.6.0"
|
||||
}
|
||||
|
||||
Then, after running `composer update` or `php composer.phar update`, you can
|
||||
@@ -881,6 +882,19 @@ S::create(' fòô bàř')->toTabs();
|
||||
S::toTabs(' fòô bàř'); // ' fòô bàř'
|
||||
```
|
||||
|
||||
#### toTitleCase
|
||||
|
||||
$stringy->toTitleCase()
|
||||
|
||||
S::toTitleCase(string $str [, string $encoding ])
|
||||
|
||||
Converts the first character of each word in the string to uppercase.
|
||||
|
||||
```php
|
||||
S::create('fòô bàř', 'UTF-8')->toTitleCase();
|
||||
S::toTitleCase('fòô bàř', 'UTF-8'); // 'Fòô Bàř'
|
||||
```
|
||||
|
||||
#### toUpperCase
|
||||
|
||||
$stringy->toUpperCase()
|
||||
|
@@ -323,6 +323,18 @@ class StaticStringy
|
||||
return (string) Stringy::create($str, $encoding)->toLowerCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of each word in the string to uppercase.
|
||||
*
|
||||
* @param string $str String to convert case
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The title-cased string
|
||||
*/
|
||||
public static function toTitleCase($str, $encoding = null)
|
||||
{
|
||||
return (string) Stringy::create($str, $encoding)->toTitleCase();
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all characters in the string to uppercase. An alias for PHP's
|
||||
* mb_strtoupper().
|
||||
|
@@ -488,6 +488,8 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
$str = str_replace($value, $key, $str);
|
||||
}
|
||||
|
||||
$str = preg_replace('/[^\x20-\x7E]/u', '', $str);
|
||||
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
@@ -674,6 +676,18 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the first character of each word in the string to uppercase.
|
||||
*
|
||||
* @return Stringy Object with all characters of $str being title-cased
|
||||
*/
|
||||
public function toTitleCase()
|
||||
{
|
||||
$str = mb_convert_case($this->str, MB_CASE_TITLE, $this->encoding);
|
||||
|
||||
return self::create($str, $this->encoding);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts all characters in the string to lowercase. An alias for PHP's
|
||||
* mb_strtolower().
|
||||
@@ -714,13 +728,12 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
|
||||
{
|
||||
$stringy = self::create($this->str, $this->encoding);
|
||||
|
||||
$stringy->str = preg_replace("/[^a-zA-Z\d $replacement]/u", '',
|
||||
$stringy->toAscii());
|
||||
$stringy->str = $stringy->collapseWhitespace()->str;
|
||||
$stringy->str = str_replace(' ', $replacement, strtolower($stringy));
|
||||
$stringy->str = preg_replace("/[$replacement]+/u", $replacement, $stringy);
|
||||
$quotedReplacement = preg_quote($replacement);
|
||||
$pattern = "/[^a-zA-Z\d\s-_$quotedReplacement]/u";
|
||||
$stringy->str = preg_replace($pattern, '', $stringy->toAscii());
|
||||
|
||||
return $stringy;
|
||||
return $stringy->toLowerCase()->applyDelimeter($replacement)
|
||||
->removeLeft($replacement)->removeRight($replacement);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -174,10 +174,11 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
return array(
|
||||
array('foo bar', 'fòô bàř'),
|
||||
array(' TEST ', ' ŤÉŚŢ '),
|
||||
array('φ = z = 3', 'φ = ź = 3'),
|
||||
array(' = z = 3', 'φ = ź = 3'),
|
||||
array('perevirka', 'перевірка'),
|
||||
array('lysaya gora', 'лысая гора'),
|
||||
array('shchuka', 'щука')
|
||||
array('shchuka', 'щука'),
|
||||
array('', '漢字')
|
||||
);
|
||||
}
|
||||
|
||||
@@ -317,6 +318,17 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
}
|
||||
|
||||
public function toTitleCaseProvider()
|
||||
{
|
||||
return array(
|
||||
array('Foo Bar', 'foo bar'),
|
||||
array(' Foo_Bar ', ' foo_bar '),
|
||||
array('Fòô Bàř', 'fòô bàř', 'UTF-8'),
|
||||
array(' Fòô_Bàř ', ' fòô_bàř ', 'UTF-8'),
|
||||
array('Αυτοκίνητο Αυτοκίνητο', 'αυτοκίνητο αυτοκίνητο', 'UTF-8'),
|
||||
);
|
||||
}
|
||||
|
||||
public function toUpperCaseProvider()
|
||||
{
|
||||
return array(
|
||||
@@ -341,11 +353,12 @@ abstract class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('numbers-1234', 'numbers 1234'),
|
||||
array('perevirka-ryadka', 'перевірка рядка'),
|
||||
array('bukvar-s-bukvoy-y', 'букварь с буквой ы'),
|
||||
// Conflicts between Russian or Bulgarian
|
||||
//array('barzi-i-yarostni', 'бързи и яростни'),
|
||||
array('podekhal-k-podezdu-moego-doma', 'подъехал к подъезду моего дома'),
|
||||
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-_', '_')
|
||||
);
|
||||
}
|
||||
|
||||
|
@@ -244,6 +244,16 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toTitleCaseProvider()
|
||||
*/
|
||||
public function testToTitleCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$result = S::toTitleCase($str, $encoding);
|
||||
$this->assertInternalType('string', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toUpperCaseProvider()
|
||||
*/
|
||||
|
@@ -432,6 +432,18 @@ class StringyTestCase extends CommonTest
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toTitleCaseProvider()
|
||||
*/
|
||||
public function testToTitleCase($expected, $str, $encoding = null)
|
||||
{
|
||||
$stringy = S::create($str, $encoding);
|
||||
$result = $stringy->toTitleCase();
|
||||
$this->assertInstanceOf('Stringy\Stringy', $result);
|
||||
$this->assertEquals($expected, $result);
|
||||
$this->assertEquals($str, $stringy);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider toUpperCaseProvider()
|
||||
*/
|
||||
|
Reference in New Issue
Block a user