mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-11-04 06:37:55 +08:00 
			
		
		
		
	Fixes #7488: Added StringHelper::explode to perform explode with trimming and skipping of empty elements
				
					
				
			This commit is contained in:
		
				
					committed by
					
						
						Alexander Makarov
					
				
			
			
				
	
			
			
			
						parent
						
							624db0e678
						
					
				
				
					commit
					12fe0c7b85
				
			@ -5,7 +5,7 @@ Yii Framework 2 Change Log
 | 
				
			|||||||
-----------------------
 | 
					-----------------------
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- Bug #7529: Fixed `yii\web\Response::sendContentAsFile()` that was broken in 2.0.3 (samdark)
 | 
					- Bug #7529: Fixed `yii\web\Response::sendContentAsFile()` that was broken in 2.0.3 (samdark)
 | 
				
			||||||
 | 
					- Enh #7488: Added `StringHelper::explode` to perform explode with trimming and skipping of empty elements (SilverFire, nineinchnick, creocoder, samdark)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
2.0.3 March 01, 2015
 | 
					2.0.3 March 01, 2015
 | 
				
			||||||
--------------------
 | 
					--------------------
 | 
				
			||||||
 | 
				
			|||||||
@ -234,4 +234,35 @@ class BaseStringHelper
 | 
				
			|||||||
            return mb_strtolower(mb_substr($string, -$bytes, null, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
 | 
					            return mb_strtolower(mb_substr($string, -$bytes, null, '8bit'), Yii::$app->charset) === mb_strtolower($with, Yii::$app->charset);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * Explodes string into array, optionally trims values and skips empty ones
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * @param string $string String to be exploded.
 | 
				
			||||||
 | 
					     * @param string $delimiter Delimiter. Default is ','.
 | 
				
			||||||
 | 
					     * @param mixed $trim Whether to trim each element. Can be:
 | 
				
			||||||
 | 
					     *   - boolean - to trim normally;
 | 
				
			||||||
 | 
					     *   - string - custom characters to trim. Will be passed as a second argument to `trim()` function.
 | 
				
			||||||
 | 
					     *   - callable - will be called for each value instead of trim. Takes the only argument - value.
 | 
				
			||||||
 | 
					     * @param boolean $skipEmpty Whether to skip empty strings between delimiters. Default is false.
 | 
				
			||||||
 | 
					     * @return array
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
 | 
					    public static function explode($string, $delimiter = ',', $trim = true, $skipEmpty = false) {
 | 
				
			||||||
 | 
					        $result = explode($delimiter, $string);
 | 
				
			||||||
 | 
					        if ($trim) {
 | 
				
			||||||
 | 
					            if ($trim === true) {
 | 
				
			||||||
 | 
					                $trim = 'trim';
 | 
				
			||||||
 | 
					            } elseif (!is_callable($trim)) {
 | 
				
			||||||
 | 
					                $trim = function($v) use ($trim) {
 | 
				
			||||||
 | 
					                    return trim($v, $trim);
 | 
				
			||||||
 | 
					                };
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            $result = array_map($trim, $result);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if ($skipEmpty) {
 | 
				
			||||||
 | 
					            // Wrapped with array_values to make array keys sequential after empty values removing
 | 
				
			||||||
 | 
					            $result = array_values(array_filter($result));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return $result;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -227,4 +227,13 @@ class StringHelperTest extends TestCase
 | 
				
			|||||||
        $this->assertTrue(StringHelper::endsWith('string', 'nG', false));
 | 
					        $this->assertTrue(StringHelper::endsWith('string', 'nG', false));
 | 
				
			||||||
        $this->assertTrue(StringHelper::endsWith('BüЯйΨ', 'ÜяЙΨ', false));
 | 
					        $this->assertTrue(StringHelper::endsWith('BüЯйΨ', 'ÜяЙΨ', false));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public function testExplode()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->assertEquals(['It', 'is', 'a first', 'test'], StringHelper::explode("It, is, a first, test"));
 | 
				
			||||||
 | 
					        $this->assertEquals(['It', 'is', 'a second', 'test'], StringHelper::explode("It+ is+ a second+ test", '+'));
 | 
				
			||||||
 | 
					        $this->assertEquals(['Save', '', '', 'empty trimmed string'], StringHelper::explode("Save, ,, empty trimmed string", ','));
 | 
				
			||||||
 | 
					        $this->assertEquals(['Здесь', 'multibyte', 'строка'], StringHelper::explode("Здесь我 multibyte我 строка", '我'));
 | 
				
			||||||
 | 
					        $this->assertEquals(['Disable', '  trim  ', 'here but ignore empty'], StringHelper::explode("Disable,  trim  ,,,here but ignore empty", ',', false, true));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user