Changeset 814


Ignore:
Timestamp:
Mar 29, 2024 10:43:45 PM (4 weeks ago)
Author:
anonymous
Message:

Add safeTrim() function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/Utilities.inc.php

    r811 r814  
    19841984    return (json_last_error() === JSON_ERROR_NONE);
    19851985}
     1986
     1987/*
     1988* Trim strings. Arrays of strings are trimmed recursively. Other data types remain untouched.
     1989*
     1990* @access   public
     1991* @param    mixed  $var A variable of any data type, which may be a multidimensional array.
     1992* @return   mixed       String values trimmed, other data types returned as-is.
     1993* @author   Quinn Comendant <quinn@strangecode.com>
     1994* @since    28 Mar 2024 20:32:29
     1995*/
     1996function safeTrim($var)
     1997{
     1998    // Directly trim if it's a string.
     1999    if (is_string($var)) {
     2000        return trim($var);
     2001    }
     2002
     2003    // Recursively trim elements if it's an array.
     2004    if (is_array($var)) {
     2005        array_walk_recursive($var, function (&$v) {
     2006            if (is_string($v)) {
     2007                $v = trim($v);
     2008            }
     2009        });
     2010        return $var;
     2011    }
     2012
     2013    // Return the value as-is for other data types.
     2014    return $var;
     2015}
Note: See TracChangeset for help on using the changeset viewer.