Created
October 3, 2015 15:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function base64_decode_maybe($str) { | |
| if (base64_encode(base64_decode($str, true)) == $str) { | |
| return base64_decode($str); | |
| } | |
| return $str; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.