Oihana PHP

normalize.php

Table of Contents

Functions

normalize()  : mixed|null
Normalizes a value according to the given cleaning flags.

Functions

normalize()

Normalizes a value according to the given cleaning flags.

normalize(mixed $value[, int $flags = CleanFlag::DEFAULT | CleanFlag::RETURN_NULL ]) : mixed|null
  • Arrays are cleaned recursively using the provided flags.
  • Strings are trimmed if CleanFlag::TRIM is set, and empty strings become null if CleanFlag::EMPTY is set.
  • Other scalar values are returned as-is, unless CleanFlag::FALSY is used.
Parameters
$value : mixed

The value to normalize (array or scalar).

$flags : int = CleanFlag::DEFAULT | CleanFlag::RETURN_NULL

A bitmask of CleanFlag values. Defaults to CleanFlag::DEFAULT | CleanFlag::RETURN_NULL.

Tags
example
normalize(['', ' foo ', null]);
// ['foo']
*
normalize
([
    'name'  => 'Alice',
    'email' => '  ',
    'age'   => null
]);
// ['name' => 'Alice']

normalize([]);
// null

 normalize(new stdClass()); // empty object
 // null

normalize(['', null, '   ']);
// null

normalize('   ');
// null

normalize('bar');
// 'bar'

normalize
([
    'zero'    => 0,
    'empty'   => '',
    'nullVal' => null,
    'ok'      => 'valid'
] , CleanFlag::FALSY | CleanFlag::RECURSIVE ) ;
// ['ok' => 'valid']

normalize
([
    'users' =>
    [
        ['name' => '', 'email' => 'bob@example.com'],
        ['name' => 'Alice', 'email' => '']
    ]
] , CleanFlag::RECURSIVE | CleanFlag::EMPTY ) ;
// [
//     'users' => [
//         ['email' => 'bob@example.com'],
//         ['name' => 'Alice']
//     ]
// ]
see
clean()

For array cleaning behavior.

see
CleanFlag

Enumeration representing cleaning modes as bit flags.

author

Marc Alcaraz (ekameleon)

since
1.0.7
Return values
mixed|null

The normalized value, cleaned array, or null if the result is empty and RETURN_NULL is set.


        
On this page

Search results