ryancooper Posted April 4, 2011 Share Posted April 4, 2011 Hi, Im trying to use base64_decode on a string, however the string is sometimes plain text others its encoded, how can i test to see if the sting is encoded or plaintext and only perform the decode if the string is indeed encoded? Im pretty new to PHP, but ive seen mentions of regex or preg_match for certain characteristics of base64 encoded, my one observation is it usually ends in == which wouldnt appear in my plain text, could i do a preg match for that? Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/ Share on other sites More sharing options...
nethnet Posted April 4, 2011 Share Posted April 4, 2011 I'm not super familiar with base64, but if each encoded string ends with ==, you could do something like the following to see if it is encoded: <?php $decoded = (preg_match('/[a-z0-9]+==/i', $stringtotest) == 1) ? base64_decode($stringtotest) : $stringtotest; ?> Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196924 Share on other sites More sharing options...
ryancooper Posted April 4, 2011 Author Share Posted April 4, 2011 Awesome, apparently there is only two equal signs if there is an extra byte left over, one if not, however this should work... Could you tell me what the code would look like with my setup? The text file is pulled in as $data, so what would i change in your example? Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196926 Share on other sites More sharing options...
nethnet Posted April 4, 2011 Share Posted April 4, 2011 Replace the three instances of $stringtotest with the variable that contains your *possible* base64 hash. I'm guessing that's your text file that you have stored in $data? If you wanted to continue to refer to it as $data after the test, replace $decoded with $data as well. Either way, the $decoded/$data variable will contain the decoded version of the base64. Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196929 Share on other sites More sharing options...
dcro2 Posted April 5, 2011 Share Posted April 5, 2011 This may work better. It also matches newlines, unless you're sure there will never be any, and makes sure the equal signs are at the end: $data = preg_match('/^[a-z0-9\r\n]+={0,2}$/i', trim($stringtotest)) ? base64_decode($stringtotest) : $stringtotest; Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196932 Share on other sites More sharing options...
ryancooper Posted April 5, 2011 Author Share Posted April 5, 2011 Awesome that worked perfectly, is the match for any = or only strings that end in = ? I realized i may use = at some point but never end with it in plain text, thanks so much for your help, couldnt of been easier, all the code ive looked at was pretty complex and above me. Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196935 Share on other sites More sharing options...
dcro2 Posted April 5, 2011 Share Posted April 5, 2011 Not sure which one you're referring to, but mine does. Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196936 Share on other sites More sharing options...
ryancooper Posted April 5, 2011 Author Share Posted April 5, 2011 Not sure which one you're referring to, but mine does. Ok thanks, i tried your version just to be safe but for some reason it's leaving the string encoded? Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196937 Share on other sites More sharing options...
dcro2 Posted April 5, 2011 Share Posted April 5, 2011 Hmm.. it seems we all missed two other valid characters in base64 (+ and /). This should work: $data = preg_match('/^[+\/a-z0-9\r\n]+={0,2}$/i', trim($stringtotest)) ? base64_decode($stringtotest) : $stringtotest; Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196938 Share on other sites More sharing options...
ryancooper Posted April 5, 2011 Author Share Posted April 5, 2011 I had to change the match to just one =. The solution from nethnet decodes fine but seems to search the whole string not just the end of it, and ive realized if if i have HTML in the sting such as a href="" then the plain text is decoded and returns null. I can get the dcro2 solution to run but it leaves the string encoded? Is it possible the match is off? Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196940 Share on other sites More sharing options...
ryancooper Posted April 5, 2011 Author Share Posted April 5, 2011 Hmm.. it seems we all missed two other valid characters in base64 (+ and /). This should work: $data = preg_match('/^[+\/a-z0-9\r\n]+={0,2}$/i', trim($stringtotest)) ? base64_decode($stringtotest) : $stringtotest; That worked perfect, thank you so much, i really appreciate it. Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196942 Share on other sites More sharing options...
dcro2 Posted April 5, 2011 Share Posted April 5, 2011 You're welcome Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196943 Share on other sites More sharing options...
requinix Posted April 5, 2011 Share Posted April 5, 2011 But "cat" isn't base64-encoded... (4 characters)* (2 characters (= | 1 character) =)? (where a "character" is not whitespace) Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196945 Share on other sites More sharing options...
dcro2 Posted April 5, 2011 Share Posted April 5, 2011 But "cat" isn't base64-encoded... (4 characters)* (2 characters (= | 1 character) =)? (where a "character" is not whitespace) Well... what can you do? It's not perfect, but I don't think there's a perfect method to detecting a base64 string. As long as the data is more than one word it works. Quote Link to comment https://forums.phpfreaks.com/topic/232709-base64-decode/#findComment-1196949 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.