How to check if a string is base64 valid in PHP

后端 未结 18 1270
执笔经年
执笔经年 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:06

    This code should work, as the decode function returns FALSE if the string is not valid:

    if (base64_decode($mystring, true)) {
        // is valid
    } else {
        // not valid
    }
    

    You can read more about the base64_decode function in the documentation.

提交回复
热议问题