Skip to content

Instantly share code, notes, and snippets.

@mdurys
Created April 4, 2023 14:42
Show Gist options
  • Select an option

  • Save mdurys/cdf1f7acab9e94b1f7f0f90598f3f319 to your computer and use it in GitHub Desktop.

Select an option

Save mdurys/cdf1f7acab9e94b1f7f0f90598f3f319 to your computer and use it in GitHub Desktop.
Check if given string is base64 encoded
private static function isBase64Encoded(string $text): bool
{
if (false === $decoded = base64_decode($text, true)) {
return false;
}
// check if decoded text consists only of unicode letters, digits and white space
if (!preg_match('|^[\p{L}\d\s]+$|u', $decoded)) {
return false;
}
return $text === base64_encode($decoded);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment