diff --git a/README.md b/README.md index 1b1a742..72f1a51 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Stringy -A PHP library with a variety of multibyte string manipulation functions. +A PHP library with a variety of multibyte string manipulation functions. Inspired by underscore.string.js. ## Usage @@ -133,15 +133,39 @@ S::titleize('i like to watch DVDs at home', 'UTF-8', $ignore); // 'I Like to Watch DVDs at Home' ``` +**humanize** + +S::humanize($string [, $encoding]) + +Capitalizes the first word of a string, replaces underscores with spaces, +and strips '_id'. + +```php +S::humanize('author_id'); // 'Author' +``` + +**tidy** + +S::tidy($string) + +Replaces smart quotes, ellipsis characters, and dashes from Windows-1252 +(and commonly used in Word documents) with their ASCII equivalents. + +```php +S::tidy('“I see…”'); // '"I see..."' +``` + ## TODO -**sentence** +**clean** + +**standardize** **center** -**endsWith** +**startsWith** -**beginsWith** +**endsWith** **toSpaces** @@ -149,19 +173,19 @@ S::titleize('i like to watch DVDs at home', 'UTF-8', $ignore); **slugify** -**contains** +**toAnchor** -**clean** +**contains** **between** **insert** -**nextChar** +**replace** -**truncateByChars** +**truncateChars** -**truncateByWords** +**truncateWords** **longestCommonPrefix** @@ -169,8 +193,6 @@ S::titleize('i like to watch DVDs at home', 'UTF-8', $ignore); **isJson** -**toAnchor** - ## Tests [![Build Status](https://travis-ci.org/danielstjules/Stringy.png)](https://travis-ci.org/danielstjules/Stringy) diff --git a/src/Stringy/Stringy.php b/src/Stringy/Stringy.php index 1353102..c1460b5 100644 --- a/src/Stringy/Stringy.php +++ b/src/Stringy/Stringy.php @@ -189,6 +189,38 @@ class Stringy { return $titleized; } + + /** + * Capitalizes the first word of a string, replaces underscores with spaces, + * and strips '_id'. + * + * @param string $string String to humanize + * @param string $encoding The character encoding + * @return string A humanized string + */ + private static function humanize($string, $encoding) { + $humanized = mb_ereg_replace('_id', '', $string); + $humanized = mb_ereg_replace('_', ' ', $humanized); + + return self::upperCaseFirst(trim($humanized), $encoding); + } + + /** + * Replaces smart quotes, ellipsis characters, and dashes from Windows-1252 + * (and commonly used in Word documents) with their ASCII equivalents. + * + * @param string $string String to remove special chars + * @param string $encoding The character encoding + * @return string String with those characters removed + */ + private static function tidy($string) { + $tidied = preg_replace('/\x{2026}/u', '...', $string); + $tidied = preg_replace('/[\x{201C}\x{201D}]/u', '"', $tidied); + $tidied = preg_replace('/[\x{2018}\x{2019}]/u', "'", $tidied); + $tidied = preg_replace('/[\x{2013}\x{2014}]/u', '-', $tidied); + + return $tidied; + } } ?> diff --git a/tests/Stringy/StringyTest.php b/tests/Stringy/StringyTest.php index 0217421..21715c2 100644 --- a/tests/Stringy/StringyTest.php +++ b/tests/Stringy/StringyTest.php @@ -210,6 +210,43 @@ class StringyTestCase extends PHPUnit_Framework_TestCase { return $testData; } + /** + * @dataProvider stringsForHumanize + */ + public function testHumanize($string, $expected, $encoding = null) { + $result = S::humanize($string, $encoding); + $this->assertEquals($expected, $result); + } + + public function stringsForHumanize() { + $testData = array( + array('author_id', 'Author'), + array(' _test_user_', 'Test user'), + array(' συγγραφέας_id ', 'Συγγραφέας', 'UTF-8') + ); + + return $testData; + } + + /** + * @dataProvider stringsForTidy + */ + public function testTidy($string, $expected) { + $result = S::tidy($string); + $this->assertEquals($expected, $result); + } + + public function stringsForTidy() { + $testData = array( + array('“I see…”', '"I see..."'), + array("‘This too’", "'This too'"), + array('test—dash', 'test-dash'), + array('Ο συγγραφέας είπε…', 'Ο συγγραφέας είπε...') + ); + + return $testData; + } + } ?>