Over a million developers have joined DZone.

How to trim recursively in PHP

· Web Dev Zone

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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;

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of Svetoslav Marinov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}