1
0
mirror of https://github.com/danielstjules/Stringy.git synced 2025-09-02 01:22:37 +02:00

Add trimLeft, trimRight, support unicode whitespace

This commit is contained in:
Daniel St. Jules
2015-07-22 13:55:21 -07:00
parent 7763df3c3b
commit fe3368bd8b
6 changed files with 194 additions and 74 deletions

View File

@@ -561,14 +561,48 @@ class StaticStringy
}
/**
* Returns the trimmed string. An alias for PHP's trim() function.
* Returns a string with whitespace removed from the start and end of the
* string. Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $str String to trim
* @param string $str String to trim
* @param string $chars Optional string of characters to strip
* @param string $encoding The character encoding
* @return string Trimmed $str
*/
public static function trim($str)
public static function trim($str, $chars = null, $encoding = null)
{
return trim($str);
return (string) Stringy::create($str, $encoding)->trim($chars);
}
/**
* Returns a string with whitespace removed from the start of the string.
* Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $str String to trim
* @param string $chars Optional string of characters to strip
* @param string $encoding The character encoding
* @return string Trimmed $str
*/
public static function trimLeft($str, $chars = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->trimLeft($chars);
}
/**
* Returns a string with whitespace removed from the end of the string.
* Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $str String to trim
* @param string $chars Optional string of characters to strip
* @param string $encoding The character encoding
* @return string Trimmed $str
*/
public static function trimRight($str, $chars = null, $encoding = null)
{
return (string) Stringy::create($str, $encoding)->trimRight($chars);
}
/**

View File

@@ -4,13 +4,6 @@ namespace Stringy;
class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
{
const TRIM_BOTH = 'trim';
const TRIM_LEFT = 'ltrim';
const TRIM_RIGHT = 'rtrim';
/**
* An instance's string.
*
@@ -1064,28 +1057,48 @@ class Stringy implements \Countable, \IteratorAggregate, \ArrayAccess
}
/**
* Returns the trimmed string.
* Returns a string with whitespace removed from the start and end of the
* string. Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $charList Characters to be removed
* @param int $type One of TRIM_BOTH, TRIM_LEFT or TRIM_RIGHT
* @param string $chars Optional string of characters to strip
* @return Stringy Object with a trimmed $str
* @throws \InvalidArgumentException
*/
public function trim($charList = " \t\n\r\0\x0B", $type = self::TRIM_BOTH)
public function trim($chars = null)
{
if (!is_string($charList)) {
throw new \InvalidArgumentException(
'Charset list must be a string'
);
}
$chars = ($chars) ? preg_quote($chars) : '[:space:]';
$validTypes = array(self::TRIM_BOTH, self::TRIM_LEFT, self::TRIM_RIGHT);
if (!in_array($type, $validTypes)) {
throw new \InvalidArgumentException('Type of trim function must ' .
'be trim (default), rtrim or ltrim, just as native php.');
}
return $this->regexReplace("^[$chars]+|[$chars]+\$", '');
}
return static::create($type($this->str, $charList), $this->encoding);
/**
* Returns a string with whitespace removed from the start of the string.
* Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $chars Optional string of characters to strip
* @return Stringy Object with a trimmed $str
*/
public function trimLeft($chars = null)
{
$chars = ($chars) ? preg_quote($chars) : '[:space:]';
return $this->regexReplace("^[$chars]+", '');
}
/**
* Returns a string with whitespace removed from the end of the string.
* Supports the removal of unicode whitespace. Accepts an optional
* string of characters to strip instead of the defaults.
*
* @param string $chars Optional string of characters to strip
* @return Stringy Object with a trimmed $str
*/
public function trimRight($chars = null)
{
$chars = ($chars) ? preg_quote($chars) : '[:space:]';
return $this->regexReplace("[$chars]+\$", '');
}
/**