mirror of
https://github.com/danielstjules/Stringy.git
synced 2025-08-11 15:54:04 +02:00
Added truncate()
This commit is contained in:
18
README.md
18
README.md
@@ -32,6 +32,7 @@ Note: The methods listed below are subject to change until we reach a 1.0.0 rele
|
||||
* [contains](#contains)
|
||||
* [surround](#surround)
|
||||
* [insert](#insert)
|
||||
* [truncate](#truncate)
|
||||
* [safeTruncate](#safetruncate)
|
||||
* [reverse](#reverse)
|
||||
* [shuffle](#shuffle)
|
||||
@@ -467,6 +468,21 @@ S::create('fòô bà', 'UTF-8')->insert('ř', 6);
|
||||
S::insert('fòô bà', 'ř', 6, 'UTF-8'); // 'fòô bàř'
|
||||
```
|
||||
|
||||
##### truncate
|
||||
|
||||
$stringy->truncate(int $length, [, string $substring = '' ])
|
||||
|
||||
S::truncate(string $str, int $length, [, string $substring = '' [, string $encoding ]])
|
||||
|
||||
Truncates $str to a given length. If $substring is provided, and
|
||||
truncating occurs, the string is further truncated so that the substring
|
||||
may be appended without exceeding the desired length.
|
||||
|
||||
```php
|
||||
S::create('What are your plans today?')->safeTruncate(19, '...');
|
||||
S::safeTruncate('What are your plans today?', 19, '...'); // 'What are your pl...'
|
||||
```
|
||||
|
||||
##### safeTruncate
|
||||
|
||||
$stringy->safeTruncate(int $length, [, string $substring = '' ])
|
||||
@@ -650,8 +666,6 @@ S::last('fòô bàř', 3, 'UTF-8'); // 'bàř'
|
||||
|
||||
**excerpt** ($str, $substring, $radius)
|
||||
|
||||
**truncate**
|
||||
|
||||
**pluralize** ($count, $singular, $plural = null)
|
||||
|
||||
**toBoolean**
|
||||
|
@@ -346,6 +346,24 @@ class StaticStringy
|
||||
return Stringy::create($str, $encoding)->insert($substring, $index)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates $str to a given length. If $substring is provided, and
|
||||
* truncating occurs, the string is further truncated so that the substring
|
||||
* may be appended without exceeding the desired length.
|
||||
*
|
||||
* @param string $str String to truncate
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @param string $encoding The character encoding
|
||||
* @return string The resulting string after truncating
|
||||
*/
|
||||
public static function truncate($str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
return Stringy::create($str, $encoding)
|
||||
->truncate($length, $substring)->str;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates the string to a given length, while ensuring that it does not
|
||||
* chop words. If $substring is provided, and truncating occurs, the string
|
||||
|
@@ -539,6 +539,30 @@ class Stringy
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates $str to a given length. If $substring is provided, and
|
||||
* truncating occurs, the string is further truncated so that the substring
|
||||
* may be appended without exceeding the desired length.
|
||||
*
|
||||
* @param int $length Desired length of the truncated string
|
||||
* @param string $substring The substring to append if it can fit
|
||||
* @return Stringy Object with the resulting $str after truncating
|
||||
*/
|
||||
public function truncate($length, $substring = '')
|
||||
{
|
||||
if ($length >= $this->length())
|
||||
return $this;
|
||||
|
||||
// Need to further trim the string so we can append the substring
|
||||
$substringLength = mb_strlen($substring, $this->encoding);
|
||||
$length = $length - $substringLength;
|
||||
|
||||
$truncated = mb_substr($this->str, 0, $length, $this->encoding);
|
||||
$this->str = $truncated . $substring;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Truncates $str to a given length, while ensuring that it does not chop
|
||||
* words. If $substring is provided, and truncating occurs, the string
|
||||
|
@@ -394,6 +394,36 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForTruncate()
|
||||
{
|
||||
$testData = array(
|
||||
array('Test foo bar', 'Test foo bar', 12),
|
||||
array('Test foo ba', 'Test foo bar', 11),
|
||||
array('Test foo', 'Test foo bar', 8),
|
||||
array('Test fo', 'Test foo bar', 7),
|
||||
array('Test', 'Test foo bar', 4),
|
||||
array('Test foo bar', 'Test foo bar', 12, '...'),
|
||||
array('Test foo...', 'Test foo bar', 11, '...'),
|
||||
array('Test ...', 'Test foo bar', 8, '...'),
|
||||
array('Test...', 'Test foo bar', 7, '...'),
|
||||
array('T...', 'Test foo bar', 4, '...'),
|
||||
array('Test fo....', 'Test foo bar', 11, '....'),
|
||||
array('Test fòô bàř', 'Test fòô bàř', 12, '', 'UTF-8'),
|
||||
array('Test fòô bà', 'Test fòô bàř', 11, '', 'UTF-8'),
|
||||
array('Test fòô', 'Test fòô bàř', 8, '', 'UTF-8'),
|
||||
array('Test fò', 'Test fòô bàř', 7, '', 'UTF-8'),
|
||||
array('Test', 'Test fòô bàř', 4, '', 'UTF-8'),
|
||||
array('Test fòô bàř', 'Test fòô bàř', 12, 'ϰϰ', 'UTF-8'),
|
||||
array('Test fòô ϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'),
|
||||
array('Test fϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'),
|
||||
array('Test ϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'),
|
||||
array('Teϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8'),
|
||||
array('What are your pl...', 'What are your plans today?', 19, '...')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
}
|
||||
|
||||
public function stringsForSafeTruncate()
|
||||
{
|
||||
$testData = array(
|
||||
@@ -417,7 +447,8 @@ class CommonTest extends PHPUnit_Framework_TestCase
|
||||
array('Test fòôϰϰ', 'Test fòô bàř', 11, 'ϰϰ', 'UTF-8'),
|
||||
array('Testϰϰ', 'Test fòô bàř', 8, 'ϰϰ', 'UTF-8'),
|
||||
array('Testϰϰ', 'Test fòô bàř', 7, 'ϰϰ', 'UTF-8'),
|
||||
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8')
|
||||
array('ϰϰ', 'Test fòô bàř', 4, 'ϰϰ', 'UTF-8'),
|
||||
array('What are your plans...', 'What are your plans today?', 22, '...')
|
||||
);
|
||||
|
||||
return $testData;
|
||||
|
@@ -231,6 +231,16 @@ class StaticStringyTestCase extends CommonTest
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForTruncate
|
||||
*/
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::truncate($str, $length, $substring, $encoding);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSafeTruncate
|
||||
*/
|
||||
|
@@ -232,12 +232,12 @@ class StringyTestCase extends CommonTest
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider stringsForSafeTruncate
|
||||
* @dataProvider stringsForTruncate
|
||||
*/
|
||||
public function testSafeTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
public function testTruncate($expected, $str, $length, $substring = '',
|
||||
$encoding = null)
|
||||
{
|
||||
$result = S::create($str, $encoding)->safeTruncate($length, $substring);
|
||||
$result = S::create($str, $encoding)->truncate($length, $substring);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user