1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-08-09 14:56:31 +02:00

Issue #82: titleize now lowercases, no longer preserves acronyms

This commit is contained in:
Daniel St. Jules
2015-07-28 22:21:33 -07:00
parent f12b40cc29
commit fa22b94abf
2 changed files with 11 additions and 10 deletions

View File

@@ -1293,31 +1293,31 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
/**
* Returns a trimmed string with the first letter of each word capitalized.
* Ignores the case of other letters, preserving any acronyms. Also accepts
* an array, $ignore, allowing you to list words not to be capitalized.
* Also accepts an array, $ignore, allowing you to list words not to be
* capitalized.
*
* @param array $ignore An array of words not to capitalize
* @return Stringy Object with a titleized $str
*/
public function titleize($ignore = null)
{
$buffer = $this->trim();
$stringy = static::create($this->trim(), $this->encoding);
$encoding = $this->encoding;
$buffer = preg_replace_callback(
$stringy->str = preg_replace_callback(
'/([\S]+)/u',
function ($match) use ($encoding, $ignore) {
if ($ignore && in_array($match[0], $ignore)) {
return $match[0];
} else {
$stringy = new Stringy($match[0], $encoding);
return (string) $stringy->upperCaseFirst();
return (string) $stringy->toLowerCase()->upperCaseFirst();
}
},
$buffer
$stringy->str
);
return new Stringy($buffer, $encoding);
return $stringy;
}
/**

View File

@@ -551,10 +551,11 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
$ignore = array('at', 'by', 'for', 'in', 'of', 'on', 'out', 'to', 'the');
return array(
array('Title Case', 'TITLE CASE'),
array('Testing The Method', 'testing the method'),
array('Testing the Method', 'testing the method', $ignore, 'UTF-8'),
array('I Like to Watch DVDs at Home', 'i like to watch DVDs at home',
$ignore, 'UTF-8'),
array('Testing the Method', 'testing the method', $ignore),
array('I Like to Watch Dvds at Home', 'i like to watch DVDs at home',
$ignore),
array('Θα Ήθελα Να Φύγει', ' Θα ήθελα να φύγει ', null, 'UTF-8')
);
}