PHP quoted_printable_encode() Function

What is PHP quoted_printable_encode() Function?

If you want to convert an 8-bit string to a quoted-printable string (a text encoding format), use PHP quoted_printable_encode() function.

Why You Need quoted_printable_encode() Function?

7-bit ASCII that is the original, standardized 128-character set. These characters include English letters, numbers, basic punctuation, and control characters. 8-bit ASCII (Extended ASCII) adds characters for foreign languages (diacritical marks), mathematical symbols, and box-drawing elements.

Email (MIME) systems were originally made for 7-bit ASCII. Outside this range, when email system transmits data, characters might corrupt. To prevent this, you can convert your 8-bit string to quoted-printable string format uses only printable ASCII characters which you can reliably transmit over 7-bit systems.

How quoted_printable_encode() Function Works?

When converting, the function follows the rules –

  • For normal printable ASCII characters: The characters remain unchanged. Ex. A remains A. check example 1.
  • For Extended ASCII (foreign languages, mathematical symbols etc): The function encodes as =HexValueOfTheChatacrer. Ex. \n remains =0A. Check example 2.
  • Line longer than 76 characters: The function wraps it. Check example 3.

When You Should Use quoted_printable_encode() Function?

You can use this function in the following situations-

  • Sending emails
  • Content that may contain UTF-8 (non-ASCII).

Syntax:

quoted_printable_encode(string)

Parameters:

The function has 1 parameter which is required-

string (Required): It specifies the input string.

Return Values:

The function returns encoded string.

Examples:

Example 1:

<?php
$string = 'Hello Learners.';
$encoded = quoted_printable_encode($string);
echo $encoded;
?>

Output:

Hello Learners.

Example 2:

<?php
$string = 'Hëllö Learners.';
$encoded = quoted_printable_encode($string);
echo $encoded;
?>

Output:

H=C3=ABll=C3=B6 Learners.

Example 3:

<?php
$string = "Hello Learners, you can learn Web Development and Web Designing from Schools of Web. The Web Development includes HTML, CSS, JavaScript, jQuery, REACT, PHP, MySQl etc.";
$encoded = quoted_printable_encode($string);
echo $encoded;
?>

Output:

Hello Learners, you can learn Web Development and Web Designing from School= s of Web. The Web Development includes HTML, CSS, JavaScript, jQuery, REACT= , PHP, MySQl etc.

Notes on quoted_printable_encode() Function:

  • The function doesn’t encrypt data, it only encodes special characters.
  • When the function converts, it follows the rules defined in RFC2045, section 6.7.
  • This function is similar to imap_8bit() except quoted_printed_encode() function doesn’t require IMAP module.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP quoted_printable_encode() Function

quoted_printable_encode() function is one of the built-in string function in PHP. Use this function to convert your usual 8-bit string to quoted-printable string.

Reference:

https://www.php.net/manual/en/function.quoted-printable-encode.php