7

How can I find whether a string is a data encoded with base64_encode() function or not?

Is it possible?

BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Richard Knop
  • 77,193
  • 144
  • 386
  • 546

2 Answers2

18

Attempt to decode it strictly against the Base64 alphabet. The second parameter allows you to enforce this strict check; by leaving it out, the decoding function simply strips out illegal characters before decoding:

if (base64_decode($str, true) === false)
{
    echo 'Not a Base64-encoded string';
}
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
1

Try this:

if(base64_encode(base64_decode($img, true)) === $img)
   echo 'is a Base64-encoded string' ;
Mahdi Bashirpour
  • 13,853
  • 11
  • 101
  • 132