Index: t3lib/class.t3lib_div.php =================================================================== --- t3lib/class.t3lib_div.php (revision 5042) +++ t3lib/class.t3lib_div.php (working copy) @@ -1645,34 +1645,35 @@ * * @param string Delimiter string to explode with * @param string The string to explode - * @param boolean If set, all empty values (='') will NOT be set in output - * @param integer If positive, the result will contain a maximum of limit elements, - * if negative, all components except the last -limit are returned, + * @param boolean If set, all empty values will be removed in output + * @param integer If positive, the result will contain a maximum of $limit elements, + * if negative, all components except the last -$limit are returned, * if zero (default), the result is not limited at all * @return array Exploded values */ - public static function trimExplode($delim, $string, $onlyNonEmptyValues = false, $limit = 0) { - $array = (!$limit ? explode($delim, $string) : explode($delim, $string, $limit)); - // for two perfomance reasons the loop is duplicated - // a) avoid check for $onlyNonEmptyValues in foreach loop - // b) avoid unnecessary code when $onlyNonEmptyValues is not set - if ($onlyNonEmptyValues) { - $new_array = array(); - foreach($array as $value) { - $value = trim($value); - if ($value != '') { - $new_array[] = $value; - } - } - // direct return for perfomance reasons - return $new_array; - } + public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0) { + $explodedValues = $limit ? explode($delim, $string, $limit) : explode($delim, $string); - foreach($array as &$value) { - $value = trim($value); + $result = array_map('trim', $explodedValues); + + if ($removeEmptyValues) { + // (1) array_filter uses the anonymous function $notEmpty to + // determine whether an element is empty and removes them + // from $result if it's empty. empty() can't be used as it + // would also sort out '0' (zero as string) (root page!) + // (2) array_values renumbers the array keys after empty entries + // were removed by array_filter + $notEmpty = create_function('$string', 'return ($string != \'\');'); + $result = array_values(array_filter($result, $notEmpty)); + + /* TODO when supporting (requiring) PHP5.3 switch $notEmpty to this: + $notEmpty = function($string) { + return ($string != ''); + } + */ } - return $array; + return $result; } /** Index: tests/t3lib/t3lib_div_testcase.php =================================================================== --- tests/t3lib/t3lib_div_testcase.php (revision 0) +++ tests/t3lib/t3lib_div_testcase.php (revision 0) @@ -0,0 +1,80 @@ + +* All rights reserved +* +* This script is part of the TYPO3 project. The TYPO3 project is +* free software; you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation; either version 2 of the License, or +* (at your option) any later version. +* +* The GNU General Public License can be found at +* http://www.gnu.org/copyleft/gpl.html. +* +* This script is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* This copyright notice MUST APPEAR in all copies of the script! +***************************************************************/ + + +/** + * Testcase for class t3lib_div + * + * @author Ingo Renner + * @package TYPO3 + * @subpackage t3lib + */ +class t3lib_div_testcase extends tx_phpunit_testcase { + + /** + * @test + */ + public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() { + $testString = ' a , b , c ,d ,, e,f,'; + $expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', ''); + $actualArray = t3lib_div::trimExplode(',', $testString); + + $this->assertEquals($expectedArray, $actualArray); + } + + /** + * @test + */ + public function checkTrimExplodeRemovesNewLines() { + $testString = ' a , b , ' . chr(10) . ' ,d ,, e,f,'; + $expectedArray = array('a', 'b', 'd', 'e', 'f'); + $actualArray = t3lib_div::trimExplode(',', $testString, true); + + $this->assertEquals($expectedArray, $actualArray); + } + + /** + * @test + */ + public function checkTrimExplodeRemovesEmptyElements() { + $testString = 'a , b , c , ,d ,, ,e,f,'; + $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f'); + $actualArray = t3lib_div::trimExplode(',', $testString, true); + + $this->assertEquals($expectedArray, $actualArray); + } + + /** + * @test + */ + public function checkTrimExplodeLimitsResults() { + $testString = ' a , b , c , , d,, ,e '; + $expectedArray = array('a', 'b', 'c , , d,, ,e'); // limiting returns the rest of the string as the last element + $actualArray = t3lib_div::trimExplode(',', $testString, false, 3); + + $this->assertEquals($expectedArray, $actualArray); + } +} + +?> \ No newline at end of file