Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to trim recursively in PHP

DZone's Guide to

How to trim recursively in PHP

· Web Dev Zone
Free Resource

Tips, tricks and tools for creating your own data-driven app, 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:

<?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;
        }
    }
}
 
?>

Explore data-driven apps with less coding and query writing, brought to you in partnership with Qlik.

Topics:

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

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}