Warning: trim() expects parameter 1 ... how to fix ?

Discussion in 'PHP' started by thesurface, Jan 6, 2012.

  1. #1
    line 438: $value = trim( $value );

    error:
    Warning: trim() expects parameter 1 to be string, array given in [path]\includes\ai\adminfunctions.php on line 438
    Code (markup):
    Full function which causes that error:
    function validate_value( &$value, $datatype, $bool_as_int = true, $username_query = true )
    {
        global $vbulletin;
        switch ( $datatype )
        {
        case "number" :
            $value += 0;
            break;
        case "integer" :
            $value = intval( $value );
            break;
        case "arrayinteger" :
            $key = array_keys( $value );
            $size = sizeOf( $key );
            $i = 0;
            while ( $i < $size )
            {
                $value[$key[$i]] = intval( $value[$key[$i]] );
                ++$i;
            }
            break;
        case "arrayfree" :
            $key = array_keys( $value );
            $size = sizeOf( $key );
            $i = 0;
            while ( $i < $size )
            {
                $value[$key[$i]] = trim( $value[$key[$i]] );
                ++$i;
            }
            break;
        case "posint" :
            $value = max( 1, intval( $value ) );
            break;
        case "boolean" :
            $value = $bool_as_int ? $value ? 1 : 0 : $value ? true : false;
            break;
        case "bitfield" :
            do
            {
                if ( !is_array( $value ) )
                {
                    break;
                }
                else
                {
                    $bitfield = 0;
                    foreach ( $value as $bitval )
                    {
                        $bitfield += $bitval;
                    }
                    $value = $bitfield;
                }
            } while ( 0 );
            $value += 0;
            break;
        case "username" :
            $value = trim( $value );
            if ( $username_query )
            {
                if ( empty( $value ) )
                {
                    $value = 0;
                }
                else if ( $userinfo = $vbulletin->db->query_first( "SELECT userid FROM ".TABLE_PREFIX."user WHERE username = '".$vbulletin->db->escape_string( htmlspecialchars_uni( $value ) )."'" ) )
                {
                    $value = $userinfo['userid'];
                }
                else
                {
                    $value = false;
                }
            }
            break;
        default :
            $value = trim( $value );
        }
        return $value;
    }
    Code (markup):

     
    thesurface, Jan 6, 2012 IP
  2. DanIonescu

    DanIonescu Peon

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It seems that $value is an array. You can modify the function to accept array parameters and handle them in a recursive manner. However, you still have to find a way to find an accurate datatype. (Btw, it's possibile that
    $datatype
    is not "arrayinteger" which doesn't return the expected result.
     
    DanIonescu, Jan 6, 2012 IP
  3. zappak

    zappak Active Member

    Messages:
    366
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    80
    #3
    get value by print_r($varible); and check on which position value is you want.. and call it by like $variable[1]; or whatever position it is ... that's it :)
     
    zappak, Jan 6, 2012 IP