diff --git a/README.md b/README.md index 72f1a51..e2fa683 100644 --- a/README.md +++ b/README.md @@ -2,9 +2,24 @@ A PHP library with a variety of multibyte string manipulation functions. Inspired by underscore.string.js. -## Usage +* [Requiring/Loading](#requiringloading) +* [Methods](#methods) + * [upperCaseFirst](#uppercasefirst) + * [lowerCaseFirst](#lowercasefirst) + * [camelize](#camelize) + * [upperCamelize](#uppercamelize) + * [dasherize](#dasherize) + * [underscored](#underscored) + * [swapCase](#swapcase) + * [titleize](#titleize) + * [humanize](#humanize) + * [tidy](#tidy) + * [clean](#clean) + * [standardize](#standardize) +* [Tests](#tests) +* [License](#license) -#### Requiring/Loading +## Requiring/Loading If you're using Composer to manage dependencies, you can include the following in your composer.json file: @@ -30,13 +45,13 @@ And in either case, I'd suggest using an alias. use Stringy\Stringy as S; ``` -#### Methods +## Methods *Note: All methods will throw a InvalidArgumentException if $string is not of type string. Furthermore, if if $encoding is not given, it defaults to mb_internal_encoding().* -**upperCaseFirst** +##### upperCaseFirst S::upperCaseFirst($string [, $encoding]) @@ -47,7 +62,7 @@ support for multibyte strings. S::upperCaseFirst('σ test', 'UTF-8'); // 'Σ test' ``` -**lowerCaseFirst** +##### lowerCaseFirst S::lowerCaseFirst($string [, $encoding]) @@ -58,7 +73,7 @@ support for multibyte strings. S::lowerCaseFirst('Σ test', 'UTF-8'); // 'σ test' ``` -**camelize** +##### camelize S::camelize($string [, $encoding]) @@ -70,7 +85,7 @@ dashes and underscores, and removes spaces, dashes, underscores. S::camelize('Camel-Case'); // 'camelCase' ``` -**upperCamelize** +##### upperCamelize S::upperCamelize($string [, $encoding]) @@ -82,7 +97,7 @@ spaces, dashes and underscores, and removes spaces, dashes, underscores. S::upperCamelize('Upper Camel-Case'); // 'UpperCamelCase' ``` -**dasherize** +##### dasherize S::dasherize($string [, $encoding]) @@ -95,7 +110,7 @@ of spaces as well as underscores. S::dasherize('TestDCase'); // 'test-d-case' ``` -**underscored** +##### underscored S::underscored($string [, $encoding]) @@ -108,7 +123,7 @@ of spaces as well as dashes. S::underscored('TestUCase'); // 'test_u_case' ``` -**swapCase** +##### swapCase S::swapCase($string [, $encoding]) @@ -118,7 +133,7 @@ Returns a case swapped version of a string. S::swapCase('Ντανιλ', 'UTF-8'); // 'νΤΑΝΙΛ' ``` -**titleize** +##### titleize S::titleize($string [, $encoding [, $ignore]]) @@ -133,7 +148,7 @@ S::titleize('i like to watch DVDs at home', 'UTF-8', $ignore); // 'I Like to Watch DVDs at Home' ``` -**humanize** +##### humanize S::humanize($string [, $encoding]) @@ -144,7 +159,7 @@ and strips '_id'. S::humanize('author_id'); // 'Author' ``` -**tidy** +##### tidy S::tidy($string) @@ -155,12 +170,29 @@ Replaces smart quotes, ellipsis characters, and dashes from Windows-1252 S::tidy('“I see…”'); // '"I see..."' ``` +##### clean + +S::clean($string) + +Trims the string and replaces consecutive whitespace characters with a +single space. + +```php +S::clean(' Ο συγγραφέας '); // 'Ο συγγραφέας' +``` + +##### standardize + +S::standardize($string) + +Converts some non-ASCII characters to their closest ASCII counterparts. + +```php +S::standardize('fòô bàř'); // 'foo bar' +``` + ## TODO -**clean** - -**standardize** - **center** **startsWith** @@ -171,21 +203,19 @@ S::tidy('“I see…”'); // '"I see..."' **toTabs** -**slugify** - **toAnchor** +**slugify** + **contains** **between** **insert** -**replace** +**truncate** -**truncateChars** - -**truncateWords** +**prune** **longestCommonPrefix** diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index c1460b5..96f1f0a 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -221,6 +221,64 @@ class Stringy { return $tidied; } + + /** + * Trims the string and replaces consecutive whitespace characters with a + * single space. + * + * @param string $string The string to cleanup whitespace + * @return string The trimmed string with condensed whitespace + */ + private static function clean($string) { + return preg_replace('/\s+/u', ' ', trim($string)); + } + + /** + * Converts some non-ASCII characters to their closest ASCII counterparts. + * + * @param string $string A string with non-ASCII characters + * @return string The string after the replacements + */ + private static function standardize($string) { + $charsArray = array( + 'a' => array('à', 'á', 'â', 'ã', 'ă', 'ä', 'å', 'ą'), + 'c' => array('ć', 'č', 'ç'), + 'd' => array('ď', 'đ'), + 'e' => array('è', 'é', 'ê', 'ě', 'ë', 'ę'), + 'g' => array('ğ'), + 'i' => array('ì', 'í', 'ï', 'î'), + 'l' => array('ĺ', 'ł'), + 'n' => array('ń', 'ñ', 'ň'), + 'o' => array('ò', 'ó', 'ô', 'õ', 'ö', 'ø'), + 'r' => array('ř', 'ŕ'), + 's' => array('š', 'š', 'ş'), + 't' => array('ť', 'ţ'), + 'u' => array('ü', 'ù', 'ú', 'û', 'µ', 'ů'), + 'y' => array('ý', 'ÿ'), + 'z' => array('ź', 'ž', 'ż'), + 'A' => array('À', 'Á', 'Â', 'Ã', 'Ä', 'Å', 'Ă', 'Ą'), + 'C' => array('Ć', 'Č', 'Ç'), + 'D' => array('Ď', 'Ð'), + 'E' => array('È', 'É', 'Ê', 'Ë', 'Ě', 'Ę'), + 'G' => array('Ğ'), + 'I' => array('Ì', 'Í', 'Ï', 'Î'), + 'L' => array('Ĺ', 'Ł'), + 'N' => array('Ń', 'Ñ', 'Ň'), + 'O' => array('Ò', 'Ó', 'Ô', 'Õ', 'Ö', 'Ø'), + 'R' => array('Ř', 'Ŕ'), + 'S' => array('Š', 'Ş', 'Ś'), + 'T' => array('Ť', 'Ţ'), + 'U' => array('Ü', 'Ù', 'Ú', 'Û', 'Ů'), + 'Y' => array('Ý', 'Ÿ'), + 'Z' => array('Ź', 'Ž', 'Ż') + ); + + foreach ($charsArray as $key => $value) { + $string = str_replace($value, $key, $string); + } + + return $string; + } } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 21715c2..77e18f1 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -247,6 +247,42 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { return $testData; } + /** + * @dataProvider stringsForClean + */ + public function testClean($string, $expected) { + $result = S::clean($string); + $this->assertEquals($expected, $result); + } + + public function stringsForClean() { + $testData = array( + array(' foo bar ', 'foo bar'), + array('test string', 'test string'), + array(' Ο συγγραφέας ', 'Ο συγγραφέας') + ); + + return $testData; + } + + /** + * @dataProvider stringsForStandardize + */ + public function testStandardize($string, $expected) { + $result = S::standardize($string); + $this->assertEquals($expected, $result); + } + + public function stringsForStandardize() { + $testData = array( + array('fòô bàř', 'foo bar'), + array(' ŤÉŚŢ ', ' TEST '), + array('φ = ź = 3', 'φ = z = 3') + ); + + return $testData; + } + } ?>