Commit 2b8cd878 authored by Carsten  Rose's avatar Carsten Rose
Browse files

OnArray.php: new function trimArray()

parent 663a25ae
...@@ -48,4 +48,19 @@ class OnArray { ...@@ -48,4 +48,19 @@ class OnArray {
return; return;
} }
/**
* Trim all elemements in an array.
* The array has to be a 1-dimensional array.
*
* @param array $arr
* @param string $character_mask
* @return array
*/
public static function trimArray(array $arr, $character_mask) {
foreach ($arr as $key => $item) {
$arr[$key] = trim($item, $character_mask);
}
return $arr;
}
} }
\ No newline at end of file
...@@ -38,4 +38,11 @@ class OnArrayTest extends \PHPUnit_Framework_TestCase { ...@@ -38,4 +38,11 @@ class OnArrayTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals(array(), $expected); $this->assertEquals(array(), $expected);
} }
public function testTrimArray() {
$raw = ['hello', '"next"', '"without trailing', 'without leading"', ' with whitespace ', '" with tick and whitespace "', ''];
$expected = ['hello', 'next', 'without trailing', 'without leading', ' with whitespace ', ' with tick and whitespace ', ''];
$this->assertEquals(\qfq\helper\OnArray::trimArray($raw, '"'), $expected);
}
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment