Detect base64 encoding in PHP?

后端 未结 12 1900
鱼传尺愫
鱼传尺愫 2020-12-05 02:10

Is there some way to detect if a string has been base64_encoded() in PHP?

We\'re converting some storage from plain text to base64 and part of it lives in a cookie

12条回答
  •  遥遥无期
    2020-12-05 02:42

    Usually a text in base64 has no spaces.

    I used this function which worked fine for me. It tests if the number of spaces in the string is less than 1 in 20.

    e.g: at least 1 space for each 20 chars --- ( spaces / strlen ) < 0.05

    function normalizaBase64($data){
        $spaces = substr_count ( $data ," ");
        if (($spaces/strlen($data))<0.05)
        {
            return base64_decode($data);
        }
        return $data;
    }
    

提交回复
热议问题