PHPBuilder - Quoted Printable encoding



RSS Twitter
Snippets Algorithms

Quoted Printable encoding

by: Guido Pietrella
|
May 22, 2001

Version: 0.1

Type: Function

Category: Algorithms

License: GNU General Public License

Description: This function converts a string to a quoted printable string. It uses PERL regular expression to do it.



<?
function QuotedPrintableEncode($sString)
{
        /* strip CR */
        $sString = preg_replace("~[\r]*~", "", $sString);

        /* encode characters */
        $sString = preg_replace("~([\x01-\x08\x10-\x1F\x3D\x7F-\xFF])~e",
                        "sprintf('=%02X', ord('\\1'))", $sString);

        /* encode blanks and tabs */
        $sString = preg_replace("~([\x09\x20])\n~e",
                        "sprintf('=%02X\n', ord('\\1'))", $sString);

        /* split string */
        $aStrParts = explode("\n", $sString);
        $nNumLines = count($aStrParts);
        for($i = 0; $i < $nNumLines; $i++)
                {
                        /* if longer than 76 adds a soft-line break */
                        if(strlen($aStrParts[$i]) > 76)
                                $aStrParts[$i] = preg_replace("~((.){73,76}((=[0-9A-Fa-f]{2})|([^=]{0,3})))~",
                                                "\\1=\n", $aStrParts[$i]);
                }

        return(implode("\r\n", $aStrParts));
}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Guido Pietrella

Comment:



Comment:

(Maximum characters: 1200). You have characters left.