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

One comment on “determine a string is base64_encoded or not in php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s