note 103278 added to function.trim
| From: | akarmenia at gmail dot com | Date: | Tue, 05 Apr 2011 13:49:13 +0000 |
| Subject: | note 103278 added to function.trim | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178513@lists.php.net to get a copy of this message | ||
Here's a function to trim strings recursively within an array.
<?php
// Trim a string or an array of strings recursively
function trim_r($array) {
if (is_string($array)) {
return trim($array);
} else if (!is_array($array)) {
return '';
}
$keys = array_keys($array);
for ($i=0; $i<count($keys); $i++) {
$key = $keys[$i];
if ( is_array($array[$key]) ) {
$array[$key] = trim_r($array[$key]);
} else if ( is_string($array[$key]) ) {
$array[$key] = trim($array[$key]);
}
}
return $array;
}
// Test
$a = array(' cat ', ' dog ' => ' mouse ', 234);
print_r(trim_r($a));
?>
Array
(
[0] => cat
[ dog ] => mouse
[1] => 234
)
----
Server IP: 69.147.83.197
Probable Submitter: 14.201.79.146
----
Manual Page -- http://www.php.net/manual/en/function.trim.php
Edit -- https://master.php.net/note/edit/103278
Del: integrated -- https://master.php.net/note/delete/103278/integrated
Del: useless -- https://master.php.net/note/delete/103278/useless
Del: bad code -- https://master.php.net/note/delete/103278/bad+code
Del: spam -- https://master.php.net/note/delete/103278/spam
Del: non-english -- https://master.php.net/note/delete/103278/non-english
Del: in docs -- https://master.php.net/note/delete/103278/in+docs
Del: other reasons-- https://master.php.net/note/delete/103278
Reject -- https://master.php.net/note/reject/103278
Search -- https://master.php.net/manage/user-notes.php