mirror of
https://github.com/processwire/processwire.git
synced 2025-08-15 03:05:26 +02:00
Update $sanitizer->trim() method to support a 3rd argument ($method) where you can specify what direction it should trim from: 'trim' (both), 'ltrim' (left), or 'rtrim' (right). Default is 'trim'.
This commit is contained in:
@@ -2927,7 +2927,7 @@ class Sanitizer extends Wire {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trim of all known UTF-8 whitespace types (or given chars) from beginning and ending of string
|
* Trim off all known UTF-8 whitespace types (or given chars) from beginning and ending of string
|
||||||
*
|
*
|
||||||
* Like PHP’s trim() but works with multibyte strings and recognizes all types of UTF-8 whitespace
|
* Like PHP’s trim() but works with multibyte strings and recognizes all types of UTF-8 whitespace
|
||||||
* as well as HTML whitespace entities. This method also optionally accepts an array for $chars argument
|
* as well as HTML whitespace entities. This method also optionally accepts an array for $chars argument
|
||||||
@@ -2939,33 +2939,44 @@ class Sanitizer extends Wire {
|
|||||||
* #pw-group-strings
|
* #pw-group-strings
|
||||||
*
|
*
|
||||||
* @param string $str
|
* @param string $str
|
||||||
* @param string|array $chars Characters to trim or omit (blank string) for all known whitespace (including UTF-8) and HTML-entity whitespace.
|
* @param string|array $chars Array or string of chars to trim, or omit (blank string) for all whitespace (includes UTF-8 and HTML-entity whitespace too).
|
||||||
|
* @param string $method Trim method, one of "trim" (both), "rtrim" (right-only) or "ltrim" (left-only). Or just "t", "r", "l" is also fine. 3.0.168+
|
||||||
* @return string
|
* @return string
|
||||||
* @since 3.0.124
|
* @since 3.0.124
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public function trim($str, $chars = '') {
|
public function trim($str, $chars = '', $method = 'trim') {
|
||||||
|
|
||||||
$str = $this->string($str);
|
$str = $this->string($str);
|
||||||
$tt = $this->getTextTools();
|
$tt = $this->getTextTools();
|
||||||
$len = $tt->strlen($str);
|
$len = $tt->strlen($str);
|
||||||
if(!$len) return $str;
|
|
||||||
if(is_array($chars) && !count($chars)) $chars = '';
|
if(!$len) return '';
|
||||||
|
|
||||||
|
$method = strtoupper($method[0]); // T, R or L
|
||||||
$trims = array();
|
$trims = array();
|
||||||
|
$str2 = '';
|
||||||
|
|
||||||
|
if(is_array($chars) && !count($chars)) $chars = '';
|
||||||
|
|
||||||
// setup trim
|
// setup trim
|
||||||
if($chars === '') {
|
if($chars === '') {
|
||||||
// default whitespace characters
|
// default whitespace characters
|
||||||
$trims = $this->getWhitespaceArray(true);
|
$trims = $this->getWhitespaceArray(true);
|
||||||
// let PHP default whitespace trim run first
|
// let PHP default whitespace trim run first
|
||||||
$str = trim($str);
|
switch($method) {
|
||||||
|
case 'R': $str = rtrim($str); break;
|
||||||
|
case 'L': $str = ltrim($str); break;
|
||||||
|
default: $str = trim($str); break;
|
||||||
|
}
|
||||||
|
$str2 = $str; // remember what it looked like here in $str2
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// user-specified characters
|
// user-specified characters
|
||||||
if(is_array($chars)) {
|
if(is_array($chars)) {
|
||||||
$trims = $chars;
|
$trims = $chars;
|
||||||
} else {
|
} else {
|
||||||
for($n = 0; $n < $tt->strlen($str); $n++) {
|
for($n = 0; $n < $tt->strlen($chars); $n++) {
|
||||||
$trim = $tt->substr($chars, $n, 1);
|
$trim = $tt->substr($chars, $n, 1);
|
||||||
$trimLen = $tt->strlen($trim);
|
$trimLen = $tt->strlen($trim);
|
||||||
if($trimLen) $trims[] = $trim;
|
if($trimLen) $trims[] = $trim;
|
||||||
@@ -2990,15 +3001,17 @@ class Sanitizer extends Wire {
|
|||||||
// at this point we know the trim character is present somewhere in the string
|
// at this point we know the trim character is present somewhere in the string
|
||||||
$trimLen = $tt->strlen($trim);
|
$trimLen = $tt->strlen($trim);
|
||||||
|
|
||||||
// while this trim character matches at beginning of string, remove it
|
// while this trim character matches at beginning of string, remove it (left trim)
|
||||||
while($trimPos === 0) {
|
if($method !== 'R') {
|
||||||
$str = $tt->substr($str, $trimLen);
|
while($trimPos === 0) {
|
||||||
$trimPos = $tt->strpos($str, $trim);
|
$str = $tt->substr($str, $trimLen);
|
||||||
$numRemovedStart++;
|
$trimPos = $tt->strpos($str, $trim);
|
||||||
|
$numRemovedStart++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// trim from end
|
// trim from end (right trim)
|
||||||
if($trimPos > 0) do {
|
if($trimPos > 0 && $method !== 'L') do {
|
||||||
$x = 0; // qty removed only in this do/while iteration
|
$x = 0; // qty removed only in this do/while iteration
|
||||||
$trimPos = $tt->strrpos($str, $trim);
|
$trimPos = $tt->strrpos($str, $trim);
|
||||||
if($trimPos === false) break;
|
if($trimPos === false) break;
|
||||||
@@ -3018,6 +3031,16 @@ class Sanitizer extends Wire {
|
|||||||
$strLen = $tt->strlen($str);
|
$strLen = $tt->strlen($str);
|
||||||
|
|
||||||
} while($numRemovedStart + $numRemovedEnd > 0 && $strLen > 0);
|
} while($numRemovedStart + $numRemovedEnd > 0 && $strLen > 0);
|
||||||
|
|
||||||
|
// if a default behavior trim and $str was modified by trimming UTF-8 or entities
|
||||||
|
// whitespaces then follow-up with a regular PHP trim, just in case
|
||||||
|
if($chars === '' && $str !== $str2) {
|
||||||
|
switch($method) {
|
||||||
|
case 'R': $str = rtrim($str); break;
|
||||||
|
case 'L': $str = ltrim($str); break;
|
||||||
|
default: $str = trim($str); break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return $str;
|
return $str;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user