How to check if a string is base64 valid in PHP

后端 未结 18 1266
执笔经年
执笔经年 2020-12-13 03:41

I have a string and want to test using PHP if it\'s a valid base64 encoded or not.

18条回答
  •  失恋的感觉
    2020-12-13 04:00

    I am using this approach. It expects the last 2 characters to be ==

    substr($buff, -2, 1) == '=' && substr($buff, -1, 1) == '=')

    Update: I ended up doing another check if the one above fails base64_decode($buff, true)

提交回复
热议问题