php imap badse64解码希伯来文

Hope someone can help. I'm writing an app that fetches emails from gmail. The emails are in Hebrew. I used This script to fetch email body. All emails have case 3 or 4 of the following script :

switch($encoding) {
    case 0: return $data; // 7BIT
    case 1: return $data; // 8BIT
    case 2: return $data; // BINARY
    case 3: return base64_decode($data); // BASE64
    case 4: return quoted_printable_decode($data); // QUOTED_PRINTABLE
    case 5: return $data; // OTHER
}

and most of emails decoded using base64_decode($data) and some using quoted_printable_decode($data).
The result I'm getting varies, some of the messages decoded properly, and some have only html text and english text decode but Hebrew comes in U+FFFD or ���� ���� ��� ���� � like this.