Miqdad's avatar

utf8 encoding and decoding in base64

I have a message table in my database with charset utf8 . Messages are saved in normal way after view months i converted the message content with base 64 encoding for saving emojis. Now when i open the previous message which are not encoded i met with this error. "Malformed UTF-8 characters, possibly incorrectly encoded" How can i retrieve my previous messages?

0 likes
2 replies
lostdreamer_nl's avatar
Level 53

base64 (encode / decode) wont give errors like this, json_encode / decode however will.

It's probable that you are decoding the assumed base64 string, and if it wasnt encoded yet, it will still decode, resulting in a non UTF8 string.

After that, you are doing some json (_decode probably) which will fail because it is not a valid UTF8 string you are passing.

You might want to try this (not 100% though as it's pretty hard to check if a string is actually base64 encoded)


function base64_decode_if_needed($str)
{
    $decoded = base64_decode($str, true);
    if($str === base64_encode($decoded)) {
        return $decoded;
    }
    return $str;
}

// use: 
$message = Message::find(1);
dd( $message->message,  base64_decode_if_needed($message->message) );

Also, you could put it in the Message model and use mutator methods (getter + setter) to do the conversion from within the model itself)

If you need more help, please post some code of where you are using it.

1 like
Miqdad's avatar

That's amazing. This solved my problem. Thanks a lot.

Please or to participate in this conversation.