Richard Knop
Richard Knop

Reputation: 83697

Is string base 64 encoded?

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

Is it possible?

Upvotes: 7

Views: 9010

Answers (2)

Mahdi Bashirpour
Mahdi Bashirpour

Reputation: 18803

Try this:

if(base64_encode(base64_decode($img, true)) === $img)
   echo 'is a Base64-encoded string' ;

Upvotes: 1

BoltClock
BoltClock

Reputation: 723578

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';
}

Upvotes: 18

Related Questions