1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-12 16:24:00 +02:00

Add humanize and tidy

This commit is contained in:
Daniel St. Jules
2013-07-12 19:35:28 -04:00
parent ff7e907b9b
commit b632268623
3 changed files with 102 additions and 11 deletions

View File

@@ -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)

View File

@@ -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;
}
}
?>

View File

@@ -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;
}
}
?>