How to check if a string is base64 valid in PHP

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

    if u are doing api calls using js for image/file upload to the back end this might help

    function is_base64_string($string)  //check base 64 encode 
    {
      // Check if there is no invalid character in string
      if (!preg_match('/^(?:[data]{4}:(text|image|application)\/[a-z]*)/', $string)){
        return false;
      }else{
        return true;
      }
    
    }
    

提交回复
热议问题