Skip to content

Instantly share code, notes, and snippets.

@RyanNutt
Created October 3, 2015 15:58
Show Gist options
  • Save RyanNutt/5d849f375e796fd0de66 to your computer and use it in GitHub Desktop.
Save RyanNutt/5d849f375e796fd0de66 to your computer and use it in GitHub Desktop.
Function to base64 decode a string if it appears to be base64 encoded, or returns the string unchanged if it doesn't look like it's encoded.
function base64_decode_maybe($str) {
if (base64_encode(base64_decode($str, true)) == $str) {
return base64_decode($str);
}
return $str;
}
@RyanNutt
Copy link
Author

RyanNutt commented Oct 3, 2015

I'd like to find a better way to check if it's encoded, but the only other solutions I've come across are regex matches looking for the right characters, and that lead to too many false positives. Decoding and encoding it back seems like an inefficient way to do this though. But, it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment