1
\$\begingroup\$

I have come up with a recursive function which applies trim() recursively to all string members of an array/object.

I don't have any specific quandaries about my code so I am receptive to all feedback.

My goal is maximum compatibility for all PHP versions: past, present, and future.

<?php
/**
 * trim_r
 * 
 * Recursively trim an array's or object's string values
 * Preserves all non-string values
 * 
 * @access  public
 * @param   mixed
 * @param   mixed
 * @return  mixed
 */
function &trim_r( &$o, $character_mask = null )
{
    // Only apply trim() to strings
    if( is_string( $o ) )
    {
        // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
        $o = ( $character_mask === null ? trim( $o ) : trim( $o, $character_mask ) );
    }
    elseif( is_array( $o ) || is_object( $o ) )
    {
        // Loop this array/object and apply trim_r() to its members
        foreach( $o as &$v )
        {
            trim_r( $v );
        }
    }

    // Supply this just in case the invoker wishes to receive result as a reference
    return $o;
}
\$\endgroup\$
2
\$\begingroup\$

Avoid if-else nesting to keep things flat. Return inside the first if is enough to do so.


I added is_iterable() so this function can handle more types than just array

/**
 * trim_r
 *
 * Recursively trim an array's or object's string values
 * Preserves all non-string values
 *
 * @access  public
 * @param   mixed
 * @param   mixed
 * @return  mixed
 */
function &trim_r( &$o, $character_mask = null )
{

    // Only apply trim() to strings
    if( is_string( $o ) )
    {
        // Respect the $character_mask; cannot pass null as 2nd parameter for some HHVM versions
        $o = trim( $o, $character_mask ?? " \t\n\r\0\x0B" );
        return $o;
    }

    if( is_iterable($o) || is_object( $o ) )
    {
        // Loop this array/object and apply trim_r() to its members
        foreach( $o as &$v )
        {
            trim_r( $v );
        }

    }

    // Supply this just in case the invoker wishes to receive result as a reference
    return $o;
}
\$\endgroup\$
  • \$\begingroup\$ Hmm, I like this but I should have specified maximum compatibility for as many PHP versions as possible. The null coalescing operator was introduced in PHP 7. As for " \t\n\r\0\x0B", I am hesitant to hardcode this because there is a chance it will change in the future just like the encoding parameter from htmlentities() \$\endgroup\$ – MonkeyZeus Nov 13 '18 at 16:41
  • \$\begingroup\$ Sorry but is_iterable() also fails to meet the compatibility criteria :-/ \$\endgroup\$ – MonkeyZeus Nov 13 '18 at 17:54
  • \$\begingroup\$ @MonkeyZeus right, I wasn't aware these is such criteria at the time of writing. \$\endgroup\$ – nforced Nov 14 '18 at 8:13
  • \$\begingroup\$ I know, that's why I upvoted you anyways \$\endgroup\$ – MonkeyZeus Nov 15 '18 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.