Php 字符串base64_编码/解码失败?
我有这个剧本:Php 字符串base64_编码/解码失败?,php,base64,Php,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
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;
}
被调用的是\u base64($input)
其中,$input
是我试图针对函数测试的两个字符串
这报告正常:!!K0deord*测试
此报告不正常:killoy2p4all
有什么区别使其返回false?您正在使用
$strict
参数对值进行解码,这意味着不需要正则表达式(如果字符串中有无效字符,解码将失败)
也不需要对解码的字符串进行编码。如果编码字符串被成功解码,那么它是有效的;再次编码不会改变任何东西的有效性
这样的内容应该足以验证base64编码的字符串:
function is_base64($s) {
return ! (base64_decode($s, true) === false);
}
它只是对值进行解码,然后返回它是否成功或失败。使用
$strict
参数进行解码时,不需要正则表达式。类似于,return!(base64_解码($s,true)==false)
应该足够了。True。见下一个答案;-)真的。非常感谢;-)