Пример #1
0
/**
 * Removes from supplied array values that begin with binary (non-character) data. 
 * Arrays may be of any depth. 
 *
 * Note that function is of limited use outside of the case it was designed for: to remove binary entries from extracted EXIF metadata arrays.
 *
 * @param array $pa_array The array to sanitize
 * @param array $pa_options
 *        allowStdClass = stdClass object array values are allowed. This is useful for arrays that are about to be passed to json_encode [Default=false]
 *		  removeNonCharacterData = remove non-character data from all array value. This option leaves all character data in-place [Default=false]
 * @return array The sanitized array
 */
function caSanitizeArray($pa_array, $pa_options = null)
{
    if (!is_array($pa_array)) {
        return array();
    }
    $vb_allow_stdclass = caGetOption('allowStdClass', $pa_options, false);
    $vb_remove_noncharacter_data = caGetOption('removeNonCharacterData', $pa_options, false);
    foreach ($pa_array as $vn_k => $vm_v) {
        if (is_array($vm_v)) {
            $pa_array[$vn_k] = caSanitizeArray($vm_v, $pa_options);
        } else {
            if ($vb_allow_stdclass && is_object($vm_v) && get_class($vm_v) == 'stdClass') {
                continue;
            }
            if (!preg_match("!^\\X+\$!", $vm_v) || !mb_detect_encoding($vm_v)) {
                unset($pa_array[$vn_k]);
                continue;
            }
            if ($vb_remove_noncharacter_data) {
                $pa_array[$vn_k] = caSanitizeStringForJsonEncode($pa_array[$vn_k]);
            }
        }
    }
    return $pa_array;
}
 public function testSanitizeStringHelper()
 {
     $this->assertEquals('test test', caSanitizeStringForJsonEncode('test test'));
     $this->assertEquals('"test" test', caSanitizeStringForJsonEncode('"test" test'));
     $this->assertEquals('(test) test', caSanitizeStringForJsonEncode('(test) test'));
 }