How to Check If String is Base64 Encoded in PHP?

03-Apr-2023

.

Admin

How to Check If String is Base64 Encoded in PHP?

Hi Dev,

This article will give you example of How to check if string is base64 encoded in PHP. we will help you to give example of base64_decode in PHP example. you can see check if a string is base64 valid in PHP. you will learn Base64 Encode Php Check With Code Examples. you will do the following things for php in check if string is base64 encoded or not.

Example 1:


index.php

<?php

$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';

echo base64_decode($str);

?>

Output:

This is an encoded string

Example 2:

index.php

<?php

$data = 'This is a test';

if (base64_decode($data, true) === false) {

echo 'Not a Base64-encoded string';

} else {

echo 'Yes it is a base64 encoded string';

}

?>

Output:

Yes it is a base64 encoded string

I hope it could help you...

#PHP