public function PHPMailer::encodeQ

5.x PHPMailer.php public PHPMailer::encodeQ($str, $position = 'text')

Encode a string using Q encoding.

Parameters

string $str the text to encode:

string $position Where the text is going to be used, see the RFC for what that means:

Return value

string

See also

http://tools.ietf.org/html/rfc2047#section-4.2

1 call to PHPMailer::encodeQ()
PHPMailer::encodeHeader in inc/PHPMailer/src/PHPMailer.php
Encode a header value (not including its label) optimally. Picks shortest of Q, B, or none. Result includes folding if needed. See RFC822 definitions for phrase, comment and text positions.

File

inc/PHPMailer/src/PHPMailer.php, line 3277

Class

PHPMailer
PHPMailer - PHP email creation and transport class.

Namespace

PHPMailer\PHPMailer

Code

public function encodeQ($str, $position = 'text') 
 {
  // There should not be any EOL in the string
  $pattern = '';
  $encoded = str_replace(["\r", "\n"], '', $str);
  switch (strtolower($position)) {
    case 'phrase':
      // RFC 2047 section 5.3
      $pattern = '^A-Za-z0-9!*+\/ -';
      break;
      /*
       * RFC 2047 section 5.2.
       * Build $pattern without including delimiters and []
       */
      /* @noinspection PhpMissingBreakStatementInspection */
    case 'comment':
      $pattern = '\(\)"';
      /* Intentional fall through */
    case 'text':
    default:
      // RFC 2047 section 5.1
      // Replace every high ascii, control, =, ? and _ characters
      /** @noinspection SuspiciousAssignmentsInspection */
      $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern;
      break;
  }
  $matches = [];
  if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
    // If the string contains an '=', make sure it's the first thing we replace
    // so as to avoid double-encoding
    $eqkey = array_search('=', $matches [0]);
    if (false !== $eqkey) {
      unset($matches [0][$eqkey]);
      array_unshift($matches [0], '=');
    }
    foreach (array_unique($matches [0]) as $char) {
      $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
    }
  }
  // Replace spaces with _ (more readable than =20)
  // RFC 2047 section 4.2(2)
  return str_replace(' ', '_', $encoded);
}