function encodeMB($str, $charset, $encoding, $prefix_len=0, $eol="\r\n")
    {
        if (!function_exists('mb_substr') || !function_exists('mb_strlen')) {
            return;
        }

        $encoding = $encoding == 'base64' ? 'B' : 'Q';
        // 75 is the value specified in the RFC
        $prefix = '=?' . $charset . '?'.$encoding.'?';
        $suffix = '?=';
        $maxLength = 75 - strlen($prefix . $suffix);

        // A multi-octet character may not be split across adjacent encoded-words
        // So, we'll loop over each character
        // mb_stlen() with wrong charset will generate a warning here and return null
        $length      = mb_strlen($str, $charset);
        $result      = '';
        $line_length = $prefix_len;

        if ($encoding == 'B') {
            // base64
            $start = 0;
            $prev  = '';

            for ($i=1; $i<=$length; $i++) {
                // See #17311
                $chunk = mb_substr($str, $start, $i-$start, $charset);
                $chunk = base64_encode($chunk);
                $chunk_len = strlen($chunk);

                if ($line_length + $chunk_len == $maxLength || $i == $length) {
                    if ($result) {
                        $result .= "\n";
                    }
                    $result .= $chunk;
                    $line_length = 0;
                    $start = $i;
                } else if ($line_length + $chunk_len > $maxLength) {
                    if ($result) {
                        $result .= "\n";
                    }
                    if ($prev) {
                        $result .= $prev;
                    }
                    $line_length = 0;
                    $start = $i - 1;
                } else {
                    $prev = $chunk;
                }
            }
        } else {
            // quoted-printable
            // see encodeQP()
            $regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';

            for ($i=0; $i<=$length; $i++) {
                $char = mb_substr($str, $i, 1, $charset);
                // RFC recommends underline (instead of =20) in place of the space
                // that's one of the reasons why we're not using iconv_mime_encode()
                if ($char == ' ') {
                    $char = '_';
                    $char_len = 1;
                } else {
                    $char = preg_replace_callback(
                        $regexp, array('Mail_mimePart', '_qpReplaceCallback'), $char
                    );
                    $char_len = strlen($char);
                }

                if ($line_length + $char_len > $maxLength) {
                    if ($result) {
                        $result .= "\n";
                    }
                    $line_length = 0;
                }

                $result      .= $char;
                $line_length += $char_len;
            }
        }

        if ($result) {
            $result = $prefix
                .str_replace("\n", $suffix.$eol.' '.$prefix, $result).$suffix;
        }

        return $result;
    }
function encodeMB($str, $charset, $encoding, $prefix_len=0, $eol="\r\n")
    {
        if (!function_exists('mb_substr') || !function_exists('mb_strlen')) {
            return;
        }

        $encoding = $encoding == 'base64' ? 'B' : 'Q';
        // 75 is the value specified in the RFC
        $prefix = '=?' . $charset . '?'.$encoding.'?';
        $suffix = '?=';
        $maxLength = 75 - strlen($prefix . $suffix);

        // A multi-octet character may not be split across adjacent encoded-words
        // So, we'll loop over each character
        // mb_stlen() with wrong charset will generate a warning here and return null
        $length      = mb_strlen($str, $charset);
        $result      = '';
        $line_length = $prefix_len;

        if ($encoding == 'B') {
            // base64
            $start = 0;
            $prev  = '';

            for ($i=1; $i<=$length; $i++) {
                // See #17311
                $chunk = mb_substr($str, $start, $i-$start, $charset);
                $chunk = base64_encode($chunk);
                $chunk_len = strlen($chunk);

                if ($line_length + $chunk_len == $maxLength || $i == $length) {
                    if ($result) {
                        $result .= "\n";
                    }
                    $result .= $chunk;
                    $line_length = 0;
                    $start = $i;
                } else if ($line_length + $chunk_len > $maxLength) {
                    if ($result) {
                        $result .= "\n";
                    }
                    if ($prev) {
                        $result .= $prev;
                    }
                    $line_length = 0;
                    $start = $i - 1;
                } else {
                    $prev = $chunk;
                }
            }
        } else {
            // quoted-printable
            // see encodeQP()
            $regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/';

            for ($i=0; $i<=$length; $i++) {
                $char = mb_substr($str, $i, 1, $charset);
                // RFC recommends underline (instead of =20) in place of the space
                // that's one of the reasons why we're not using iconv_mime_encode()
                if ($char == ' ') {
                    $char = '_';
                    $char_len = 1;
                } else {
                    $char = preg_replace_callback(
                        $regexp, array('Mail_mimePart', '_qpReplaceCallback'), $char
                    );
                    $char_len = strlen($char);
                }

                if ($line_length + $char_len > $maxLength) {
                    if ($result) {
                        $result .= "\n";
                    }
                    $line_length = 0;
                }

                $result      .= $char;
                $line_length += $char_len;
            }
        }

        if ($result) {
            $result = $prefix
                .str_replace("\n", $suffix.$eol.' '.$prefix, $result).$suffix;
        }

        return $result;
    }
function encodeMB ( $ str , $ charset , $ encoding , $ prefix_len = 0 , $ eol = "\r\n" ) { if ( ! function_exists ( 'mb_substr' ) || ! function_exists ( 'mb_strlen' ) ) { return ; } $ encoding = $ encoding == 'base64' ? 'B' : 'Q' ; // 75 is the value specified in the RFC $ prefix = '=?' . $ charset . '?' . $ encoding . '?' ; $ suffix = '?=' ; $ maxLength = 75 - strlen ( $ prefix . $ suffix ) ; // A multi-octet character may not be split across adjacent encoded-words // So, we'll loop over each character // mb_stlen() with wrong charset will generate a warning here and return null $ length = mb_strlen ( $ str , $ charset ) ; $ result = '' ; $ line_length = $ prefix_len ; if ( $ encoding == 'B' ) { // base64 $ start = 0 ; $ prev = '' ; for ( $ i = 1 ; $ i <= $ length ; $ i ++ ) { // See #17311 $ chunk = mb_substr ( $ str , $ start , $ i - $ start , $ charset ) ; $ chunk = base64_encode ( $ chunk ) ; $ chunk_len = strlen ( $ chunk ) ; if ( $ line_length + $ chunk_len == $ maxLength || $ i == $ length ) { if ( $ result ) { $ result .= "\n" ; } $ result .= $ chunk ; $ line_length = 0 ; $ start = $ i ; } else if ( $ line_length + $ chunk_len > $ maxLength ) { if ( $ result ) { $ result .= "\n" ; } if ( $ prev ) { $ result .= $ prev ; } $ line_length = 0 ; $ start = $ i - 1 ; } else { $ prev = $ chunk ; } } } else { // quoted-printable // see encodeQP() $ regexp = '/([\x22-\x29\x2C\x2E\x3A-\x40\x5B-\x60\x7B-\x7E\x80-\xFF])/' ; for ( $ i = 0 ; $ i <= $ length ; $ i ++ ) { $ char = mb_substr ( $ str , $ i , 1 , $ charset ) ; // RFC recommends underline (instead of =20) in place of the space // that's one of the reasons why we're not using iconv_mime_encode() if ( $ char == ' ' ) { $ char = '_' ; $ char_len = 1 ; } else { $ char = preg_replace_callback ( $ regexp , array ( 'Mail_mimePart' , '_qpReplaceCallback' ) , $ char ) ; $ char_len = strlen ( $ char ) ; } if ( $ line_length + $ char_len > $ maxLength ) { if ( $ result ) { $ result .= "\n" ; } $ line_length = 0 ; } $ result .= $ char ; $ line_length += $ char_len ; } } if ( $ result ) { $ result = $ prefix . str_replace ( "\n" , $ suffix . $ eol . ' ' . $ prefix , $ result ) . $ suffix ; } return $ result ; }
Encodes the given string using base64 or quoted-printable.
This method makes sure that encoded-word represents an integral
number of characters as per RFC2047.

@param string $str        String to encode
@param string $charset    Character set name
@param string $encoding   Encoding name (base64 or quoted-printable)
@param int    $prefix_len Prefix length. Default: 0
@param string $eol        End-of-line sequence. Default: "\r\n"

@return string     Encoded string
@access public
@since 1.8.0
Encodes the given string using base64 or quoted - printable . This method makes sure that encoded - word represents an integral number of characters as per RFC2047 .