我有一个字符串,希望使用PHP测试它是否是有效的base64编码。
我知道这个话题很老了,但是使用严格参数并不一定会有帮助。
对于像"I am not base 64 encoded"这样的字符串运行base64_decode不会返回false。
然而,如果你尝试使用严格模式解码字符串,并用base64_encode重新编码它,你可以将结果与原始数据进行比较,以确定是否为有效的base64编码值:
if ( base64_encode(base64_decode($data, true)) === $data){
echo '$data is valid';
} else {
echo '$data is NOT valid';
}
$data
没有有效字符,这将生成一个警告,因为第二个base64_decode
将返回FALSE,而第一个将把false
编码为布尔值。 base64_decode(false) === $data
,因此建议加上@以防止警告。 - Zerquix18如果 ( base64_encode(base64_encode(base64_decode(base64_decode($data, true)))) === $data)
- Tiamiyu Saheed Oluwatosin function is_base64($s)
{
return (bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s);
}
仅适用于字符串,您可以使用此函数,在返回 true 之前检查多个 base64 属性:
function is_base64($s){
// Check if there are valid base64 characters
if (!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s)) return false;
// Decode the string in strict mode and check the results
$decoded = base64_decode($s, true);
if(false === $decoded) return false;
// Encode the string again
if(base64_encode($decoded) != $s) return false;
return true;
}
这段代码应该可以正常工作,因为如果字符串无效,decode函数会返回FALSE:
if (base64_decode($mystring, true)) {
// is valid
} else {
// not valid
}
strict: 如果输入包含来自base64字母表之外的字符,则返回FALSE
。我不知道为什么 PHP 决定以这种方式处理它,但无论如何,它并不能真正检测出 base64 编码。Kris 的回答是正确的。 - Ben D我认为唯一的方法就是使用 $strict
参数设置为 true
的 base64_decode()
函数,然后查看它是否返回 false
。
我在我的项目中测试了这个方法,它完美地工作。当你将Base64图像传递给这个方法时,如果它有效,它将返回true,否则返回false。让我们试一下并告诉我是否有错误。我会在未来进行编辑和学习。
/**
* @param $str
* @return bool
*/
private function isValid64base($str){
if (base64_decode($str, true) !== false){
return true;
} else {
return false;
}
}
这是一个很老的问题,但我发现以下方法实际上是非常可靠的。它还考虑了那些包含无效字符的奇怪字符串,在验证时可能导致异常。
public static function isBase64Encoded($str)
{
try
{
$decoded = base64_decode($str, true);
if ( base64_encode($decoded) === $str ) {
return true;
}
else {
return false;
}
}
catch(Exception $e)
{
// If exception is caught, then it is not a base64 encoded string
return false;
}
}
我从这个页面得到了灵感,并将其适应到了PHP中。
public function IsBase64($data) {
$decoded_data = base64_decode($data, true);
$encoded_data = base64_encode($decoded_data);
if ($encoded_data != $data) return false;
else if (!ctype_print($decoded_data)) return false;
return true;
}
以上代码可能会返回意外的结果。例如,对于字符串“json”,它将返回false。因为“json”中的字符数量是4的倍数,并且所有字符都在允许的base64编码字符串范围内,所以“json”可能是一个有效的base64编码字符串。似乎我们必须知道原始字符串允许的字符范围,然后检查解码数据是否具有这些字符。
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;
}
}
好的,大家...终于我找到了一个针对此问题的铁板钉钉的解决方案。使用以下函数检查字符串是否进行了base64编码 -
private function is_base64_encoded($str) {
$decoded_str = base64_decode($str);
$Str1 = preg_replace('/[\x00-\x1F\x7F-\xFF]/', '', $decoded_str);
if ($Str1!=$decoded_str || $Str1 == '') {
return false;
}
return true;
}
base64_encode(base64_decode($data, true)) === $data
技巧。请参见以下评论:Amir's answer @ Detect base64 encoding in PHP?,其中详细讨论了它的多种失败方式和原因。 - mickmackusa