Over a million developers have joined DZone.

How to trim recursively in PHP

DZone's Guide to

How to trim recursively in PHP

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

I have started filling the content of one of my sites usage.cc.

So here is how how to trim recursively in PHP:

// Usage:
echo Our_Util_String::trim('test');
 * Util string functions.
 * @author Svetoslav Marinov <svetoslavm@gmail.com>
 * @copyright Svetoslav Marinov <svetoslavm@gmail.com> & others
 * @version 1.0
class Our_Util_String {
     * Recursive Trimmer <img src="http://usage.cc/wp-includes/images/smilies/icon_smile.gif" alt=":)" class="wp-smiley">
     * http://php.net/manual/en/function.trim.php
     * @param mixed $arr
     * @param string $charlist
     * @return mixed
    function trim($arr, $charlist = ' ') {
        if (is_string($arr)) {
            return trim($arr, $charlist);
        } elseif (is_array($arr)) {
            foreach($arr as $key => $value){
                if (is_array($value)) {
                    $result[$key] = self::trim($value, $charlist);
                } else {
                    $result[$key] = trim($value, $charlist);
            return $result;
        } else {
            return $arr;

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}