mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-13 16:53:59 +02:00
Update readme with todo items, rename truncate to safeTruncate, and clean to collapseWhitespace
This commit is contained in:
64
README.md
64
README.md
@@ -2,6 +2,8 @@
|
|||||||
|
|
||||||
A PHP library with a variety of string manipulation functions with multibyte support. Inspired by underscore.string.js.
|
A PHP library with a variety of string manipulation functions with multibyte support. Inspired by underscore.string.js.
|
||||||
|
|
||||||
|
Note: The methods listed below are subject to change until we reach a 1.0.0 release.
|
||||||
|
|
||||||
* [Requiring/Loading](#requiringloading)
|
* [Requiring/Loading](#requiringloading)
|
||||||
* [Methods](#methods)
|
* [Methods](#methods)
|
||||||
* [upperCaseFirst](#uppercasefirst)
|
* [upperCaseFirst](#uppercasefirst)
|
||||||
@@ -14,7 +16,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
|||||||
* [titleize](#titleize)
|
* [titleize](#titleize)
|
||||||
* [humanize](#humanize)
|
* [humanize](#humanize)
|
||||||
* [tidy](#tidy)
|
* [tidy](#tidy)
|
||||||
* [clean](#clean)
|
* [collapseWhitespace](#collapsewhitespace)
|
||||||
* [standardize](#standardize)
|
* [standardize](#standardize)
|
||||||
* [pad](#pad)
|
* [pad](#pad)
|
||||||
* [padLeft](#padleft)
|
* [padLeft](#padleft)
|
||||||
@@ -28,7 +30,7 @@ A PHP library with a variety of string manipulation functions with multibyte sup
|
|||||||
* [contains](#contains)
|
* [contains](#contains)
|
||||||
* [surround](#surround)
|
* [surround](#surround)
|
||||||
* [insert](#insert)
|
* [insert](#insert)
|
||||||
* [truncate](#truncate)
|
* [safeTruncate](#safetruncate)
|
||||||
* [reverse](#reverse)
|
* [reverse](#reverse)
|
||||||
* [shuffle](#shuffle)
|
* [shuffle](#shuffle)
|
||||||
* [Tests](#tests)
|
* [Tests](#tests)
|
||||||
@@ -183,15 +185,15 @@ Replaces smart quotes, ellipsis characters, and dashes from Windows-1252
|
|||||||
S::tidy('“I see…”'); // '"I see..."'
|
S::tidy('“I see…”'); // '"I see..."'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### clean
|
##### collapseWhitespace
|
||||||
|
|
||||||
S::clean(string $str)
|
S::collapseWhitespace(string $str)
|
||||||
|
|
||||||
Trims the string and replaces consecutive whitespace characters with a
|
Trims the string and replaces consecutive whitespace characters with a
|
||||||
single space.
|
single space. This inclues tabs and newline characters.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
S::clean(' Ο συγγραφέας '); // 'Ο συγγραφέας'
|
S::collapseWhitespace(' Ο συγγραφέας '); // 'Ο συγγραφέας'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### standardize
|
##### standardize
|
||||||
@@ -340,9 +342,9 @@ Inserts $substring into $str at the $index provided.
|
|||||||
S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
|
S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### truncate
|
##### safeTruncate
|
||||||
|
|
||||||
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
S::safeTruncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
||||||
|
|
||||||
Truncates the string to a given length, while ensuring that it does not
|
Truncates the string to a given length, while ensuring that it does not
|
||||||
chop words. If $substring is provided, and truncating occurs, the string
|
chop words. If $substring is provided, and truncating occurs, the string
|
||||||
@@ -350,7 +352,7 @@ is further truncated so that the substring may be appended without
|
|||||||
exceeding the desired length.
|
exceeding the desired length.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
S::truncate('What are your plans today?', 22, '...'); // 'What are your plans...'
|
S::safeTruncate('What are your plans today?', 22, '...'); // 'What are your plans...'
|
||||||
```
|
```
|
||||||
|
|
||||||
##### reverse
|
##### reverse
|
||||||
@@ -386,6 +388,50 @@ S::shuffle('fòô bàř', 'UTF-8') // 'àôřb òf'
|
|||||||
|
|
||||||
**isMultibyte**
|
**isMultibyte**
|
||||||
|
|
||||||
|
**wordWrap**
|
||||||
|
|
||||||
|
**chars** $callback
|
||||||
|
|
||||||
|
**words** $callback
|
||||||
|
|
||||||
|
**paragraphs**
|
||||||
|
|
||||||
|
**lines**
|
||||||
|
|
||||||
|
**excerpt** ($str, $substring, $radius)
|
||||||
|
|
||||||
|
**truncate**
|
||||||
|
|
||||||
|
**pluralize** ($count, $singular, $plural = null)
|
||||||
|
|
||||||
|
**at** $position
|
||||||
|
|
||||||
|
**first**
|
||||||
|
|
||||||
|
**last**
|
||||||
|
|
||||||
|
**from**
|
||||||
|
|
||||||
|
**to**
|
||||||
|
|
||||||
|
**toBoolean**
|
||||||
|
|
||||||
|
**ensureLeft**
|
||||||
|
|
||||||
|
**ensureRight**
|
||||||
|
|
||||||
|
**isAlpha**
|
||||||
|
|
||||||
|
**isAlphaNumeric**
|
||||||
|
|
||||||
|
**isUpper**
|
||||||
|
|
||||||
|
**isLower**
|
||||||
|
|
||||||
|
**isBlank**
|
||||||
|
|
||||||
|
**length**
|
||||||
|
|
||||||
## Tests
|
## Tests
|
||||||
|
|
||||||
[](https://travis-ci.org/danielstjules/Stringy)
|
[](https://travis-ci.org/danielstjules/Stringy)
|
||||||
|
@@ -217,12 +217,12 @@ class Stringy
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Trims the string and replaces consecutive whitespace characters with a
|
* Trims the string and replaces consecutive whitespace characters with a
|
||||||
* single space.
|
* single space. This inclues tabs and newline characters.
|
||||||
*
|
*
|
||||||
* @param string $str The string to cleanup whitespace
|
* @param string $str The string to cleanup whitespace
|
||||||
* @return string The trimmed string with condensed whitespace
|
* @return string The trimmed string with condensed whitespace
|
||||||
*/
|
*/
|
||||||
public static function clean($str)
|
public static function collapseWhitespace($str)
|
||||||
{
|
{
|
||||||
return preg_replace('/\s+/u', ' ', trim($str));
|
return preg_replace('/\s+/u', ' ', trim($str));
|
||||||
}
|
}
|
||||||
@@ -482,7 +482,7 @@ class Stringy
|
|||||||
public static function slugify($str)
|
public static function slugify($str)
|
||||||
{
|
{
|
||||||
$str = preg_replace('/[^a-zA-Z\d -]/u', '', self::standardize($str));
|
$str = preg_replace('/[^a-zA-Z\d -]/u', '', self::standardize($str));
|
||||||
$str = self::clean($str);
|
$str = self::collapseWhitespace($str);
|
||||||
|
|
||||||
return str_replace(' ', '-', strtolower($str));
|
return str_replace(' ', '-', strtolower($str));
|
||||||
}
|
}
|
||||||
@@ -551,7 +551,7 @@ class Stringy
|
|||||||
* @param string $encoding The character encoding
|
* @param string $encoding The character encoding
|
||||||
* @return string The resulting string after truncating
|
* @return string The resulting string after truncating
|
||||||
*/
|
*/
|
||||||
public static function truncate($str, $length, $substring = '',
|
public static function safeTruncate($str, $length, $substring = '',
|
||||||
$encoding = null)
|
$encoding = null)
|
||||||
{
|
{
|
||||||
$encoding = $encoding ?: mb_internal_encoding();
|
$encoding = $encoding ?: mb_internal_encoding();
|
||||||
|
@@ -263,15 +263,15 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider stringsForClean
|
* @dataProvider stringsForCollapseWhitespace
|
||||||
*/
|
*/
|
||||||
public function testClean($expected, $string)
|
public function testCollapseWhitespace($expected, $string)
|
||||||
{
|
{
|
||||||
$result = S::clean($string);
|
$result = S::collapseWhitespace($string);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stringsForClean()
|
public function stringsForCollapseWhitespace()
|
||||||
{
|
{
|
||||||
$testData = array(
|
$testData = array(
|
||||||
array('foo bar', ' foo bar '),
|
array('foo bar', ' foo bar '),
|
||||||
@@ -624,16 +624,16 @@ class StringyTestCase extends PHPUnit_Framework_TestCase
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dataProvider stringsForTruncate
|
* @dataProvider stringsForSafeTruncate
|
||||||
*/
|
*/
|
||||||
public function testTruncate($expected, $string, $length, $substring = '',
|
public function testSafeTruncate($expected, $string, $length, $substring = '',
|
||||||
$encoding = null)
|
$encoding = null)
|
||||||
{
|
{
|
||||||
$result = S::truncate($string, $length, $substring, $encoding);
|
$result = S::safeTruncate($string, $length, $substring, $encoding);
|
||||||
$this->assertEquals($expected, $result);
|
$this->assertEquals($expected, $result);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function stringsForTruncate()
|
public function stringsForSafeTruncate()
|
||||||
{
|
{
|
||||||
$testData = array(
|
$testData = array(
|
||||||
array('Test foo bar', 'Test foo bar', 12),
|
array('Test foo bar', 'Test foo bar', 12),
|
||||||
|
Reference in New Issue
Block a user