PHPMailer\PHPMailer

PHPMailer::utf8CharBoundary()publicWP 1.0

Find the last character boundary prior to $maxLength in a utf-8 quoted-printable encoded string. Original written by Colin Brown.

Method of the class: PHPMailer{}

No Hooks.

Return

Int.

Usage

$PHPMailer = new PHPMailer();
$PHPMailer->utf8CharBoundary( $encodedText, $maxLength );
$encodedText(string) (required)
utf-8 QP text
$maxLength(int) (required)
Find the last character boundary prior to this length

PHPMailer::utf8CharBoundary() code WP 6.6.2

public function utf8CharBoundary($encodedText, $maxLength)
{
    $foundSplitPos = false;
    $lookBack = 3;
    while (!$foundSplitPos) {
        $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
        $encodedCharPos = strpos($lastChunk, '=');
        if (false !== $encodedCharPos) {
            //Found start of encoded character byte within $lookBack block.
            //Check the encoded byte value (the 2 chars after the '=')
            $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
            $dec = hexdec($hex);
            if ($dec < 128) {
                //Single byte character.
                //If the encoded char was found at pos 0, it will fit
                //otherwise reduce maxLength to start of the encoded char
                if ($encodedCharPos > 0) {
                    $maxLength -= $lookBack - $encodedCharPos;
                }
                $foundSplitPos = true;
            } elseif ($dec >= 192) {
                //First byte of a multi byte character
                //Reduce maxLength to split at start of character
                $maxLength -= $lookBack - $encodedCharPos;
                $foundSplitPos = true;
            } elseif ($dec < 192) {
                //Middle byte of a multi byte character, look further back
                $lookBack += 3;
            }
        } else {
            //No encoded character found
            $foundSplitPos = true;
        }
    }

    return $maxLength;
}