I have a regex designed to detect plausible Base64 strings. It works in tests at https://regex101.com for all expected test values.
~^((?:[a-zA-Z0-9/+]{4})*(?:(?:[a-zA-Z0-9/+]{3}=)|(?:[a-zA-Z0-9/+]{2}==))?)$~
However, when I use this pattern in PHP, I find some values inexplicably fail.
$tests = array(
'MFpGQkVBJTNkJTNkfTxCUj4NCg0KICAgIDwvZm9udD4=',
'MFpGRkVBJTNkJTNkfTxCUj4NCg0KICAgIDwvZm9udD4=',
'MFpGSkVBJTNkJTNkfTxCUj4NCg0KICAgIDwvZm9udD4=',
);
foreach ($tests as $str) {
$result = preg_match(
'~^((?:[a-zA-Z0-9/+]{4})*(?:(?:[a-zA-Z0-9/+]{3}=)|(?:[a-zA-Z0-9/+]{2}==))?)$~i',
preg_replace('~[\s\R]~u', "", $str)
);
var_dump($result);
}
results:
int(1)
int(0)
int(1)
Question: Why does this pattern fail for the second test string?