determine a string is base64_encoded or not in php

I can easily find out if a string is encoded in base64? Basically, if the string is base64 encoded It must be an exact multiple of 4 characters. (Remember that it may end in1, 2 or 3 ‘=’ characters.) We can check this using the regular expression

Here is the example what i do

if(preg_match(‘%^[a-zA-Z0-9/+]*=%’, $data))
{
$data1 = $data;
}
else
{
$data1 = base64_encode($data);

}

I hope this help you a little bit 🙂

Advertisements