Skip to content

Instantly share code, notes, and snippets.

@gwarnants
Last active July 9, 2024 08:18
Show Gist options
  • Save gwarnants/2048791 to your computer and use it in GitHub Desktop.
Save gwarnants/2048791 to your computer and use it in GitHub Desktop.
Check if a string seems to be base64 encoded
<?php
/**
* @param string $str
* @return bool
*/
function is_base64($str)
{
return (bool)preg_match('`^[a-zA-Z0-9+/]+={0,2}$`', $str);
}
@SerialPort-zz
Copy link

Regards.
I need just detect when a short string is encoded as base64, but your function no work:

    echo (bool)preg_match('`^[a-zA-Z0-9+/]+={0,2}$`', '4578');

Thanks

@zahmaci
Copy link

zahmaci commented May 28, 2015

Maybe this better

<?php
function is_base64($str){
    if ( base64_encode(base64_decode($str, true)) === $str){
       return true;
    } else {
       return false;
    }
}

@Robdebert
Copy link

base64_encode("435435") does not return any "=" chars. So this function will not work for some input.

@iNilo
Copy link

iNilo commented Mar 1, 2016

Maybe this better

string(4) "nope"
bool(true)

@gnanet
Copy link

gnanet commented Jan 14, 2018

string(4) "nope" can be base64 decoded ... so it works if you base64 decode and reencode the input and check if they are the same :D

@ZakharovAndrew
Copy link

Use this:

if (base64_decode($mystring, true)) {
    // is valid
} else {
    // not valid
}

@oleg-moseyko
Copy link

Use this:

if (base64_decode($mystring, true)) {
    // is valid
} else {
    // not valid
}

$mystring = 'aaa';
result is true

@nivekalara237
Copy link

use this:


<?php

 $validB64 = preg_match("/data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+).base64,.*/", $base64_fille);


@morcegon
Copy link

Maybe this better

<?php
function is_base64($str){
    if ( base64_encode(base64_decode($str, true)) === $str){
       return true;
    } else {
       return false;
    }
}

Thanks bro!!!!

@desaikalpesh34
Copy link

function is_base64($str){
    if ( base64_encode(base64_decode($str, true)) === $str){
       return true;
    } else {
       return false;
    }
}

is_base64('Matthies') or is_base64('Barthels') return true

this is real name and function fails!!!!

@mariojrrc
Copy link

mariojrrc commented Sep 1, 2020

function is_base64($str){
    if ( base64_encode(base64_decode($str, true)) === $str){
       return true;
    } else {
       return false;
    }
}

is_base64('Matthies') or is_base64('Barthels') return true

this is real name and function fails!!!!

@desaikalpesh34 I believe this solve your problem and mine!

private function isBase64Encoded(string $s) : bool
    {
        if ((bool) preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $s) === false) {
            return false;
        }
        $decoded = base64_decode($s, true);
        if ($decoded === false) {
            return false;
        }
        $encoding = mb_detect_encoding($decoded);
        if (! in_array($encoding, ['UTF-8', 'ASCII'], true)) {
            return false;
        }
        return $decoded !== false && base64_encode($decoded) === $s;
    }

@colzzky
Copy link

colzzky commented Sep 8, 2022

mariojrrc

Dude.. i've searched far and wide - and your solution is the only one that "TRULY" works..
Thanks mate!

@mariojrrc
Copy link

Glad to help! @colzzky :)

@sorwaar
Copy link

sorwaar commented Jan 17, 2023

Maybe this better

<?php
function is_base64($str){
    if ( base64_encode(base64_decode($str, true)) === $str){
       return true;
    } else {
       return false;
    }
}

Thanks bro!!!!

Perfect

@netamity
Copy link

netamity commented Jul 9, 2024

None of the answers worked for me. In my case it was the lack of spaces that I used to tell if it was base64 encoded
$teststring = substr($string,4,100);
$not64 = strpos($teststring," ");
It's not in a function, but it has worked for me. I'm sure people will tell me that it won't always work, but ALL of the functions suggested above returned false for me regardless of the string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment