View Single Post
  #4 (permalink)  
Old 07-07-09, 05:48 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 2,838
Thanks: 13
Thanked 11 Times in 10 Posts
Try this:


base64_decode — Decodes data encoded with MIME base64
PHP: base64_decode - Manual

Usage:
string base64_decode ( string $data [, bool $strict= false ] )

data - The encoded data.
strict - Returns FALSE if input contains character from outside the base64 alphabet.

PHP Code:
$str 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo 
base64_decode($str);
?> 


You might also use this regular expression:

^[a-zA-Z0-9/+]*={0,2}$

Which will also detect the usage of = or == at the end of the string (and only end). A function geared specifically toward this:

PHP Code:
<?php

function is_base64_encoded()
    {
        if (
preg_match('%^[a-zA-Z0-9/+]*={0,2}$%'$data)) {
            return 
TRUE;
        } else {
            return 
FALSE;
        }
    };

is_base64_encoded("iash21iawhdj98UH3"); // true
is_base64_encoded("#iu3498r"); // false
is_base64_encoded("asiudfh9w=8uihf"); // false
is_base64_encoded("a398UIhnj43f/1!+sadfh3w84hduihhjw=="); // true

?>
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data (scroll down)
Reply With Quote