我的一亩三分地 我就喜欢!
13fen  设为主页
 收藏本站
 
当前位置: > 一亩三分地:首页 > 网络学院 > 网络编程 > PHP专区 > php深入/精通 > 使用php的编码功能-mime.inc(2)
热门文章排行
热门文章排行 检查email地址格式的代码(01-11)
PHP操作文件问答(01-11)
PHP安装攻略:安装并配置PHP(10-23)
PHP的十个高级技巧 4(10-23)
PHP控制语句(10-12)
精采文章排行
精采文章排行 PHP连接MYSQL的两种方法(11-15)
PHP和MySQL开发的8个技巧(10-23)
PHP安装攻略:安装并配置PHP(10-23)
php+mysql扎实个人基本功(10-23)
PHP编程常用技巧四则(10-23)
技术专题推荐
网管论坛交流
 

使用php的编码功能-mime.inc(2) 

作者:佚名   来源:本站教程   点击:   日期:2007-01-11

<?php
// $Horde: horde/lib/MIME.php,v 1.63 2001/08/08 21:00:27 chuck Exp $

$mime_types =
array(
      TYPETEXT => 'text', 'text' => TYPETEXT,
      TYPEMULTIPART => 'multipart', 'multipart' => TYPEMULTIPART,
      TYPEMESSAGE => 'message', 'message' => TYPEMESSAGE,
      TYPEAPPLICATION => 'application', 'application' => TYPEAPPLICATION,
      TYPEAUDIO => 'audio', 'audio' => TYPEAUDIO,
      TYPEIMAGE => 'image', 'image' => TYPEIMAGE,
      TYPEVIDEO => 'video', 'video' => TYPEVIDEO,
      TYPEOTHER => 'unknown', 'unknown' => TYPEOTHER
      );

$mime_encodings =
array(
      ENC7BIT => '7bit', '7bit' => ENC7BIT,
      ENC8BIT => '8bit', '8bit' => ENC8BIT,
      ENCBINARY => 'binary', 'binary' => ENCBINARY,
      ENCBASE64 => 'base64', 'base64' => ENCBASE64,
      ENCQUOTEDPRINTABLE => 'quoted-printable', 'quoted-printable' => ENCQUOTEDPRINTABLE,
      ENCOTHER => 'unknown', 'unknown' => ENCOTHER
      );


/**
* The MIME:: class provides methods for dealing with MIME standards.
*
* @author  Chuck Hagenbuch <chuck@horde.org>
* @version $Revision: 1.64 $
* @since   Horde 1.3
* @package horde.mime
*/
class MIME {
    
    /**
     * Determine if a string contains 8-bit characters.
     * @access private
     *
     * @param string $string  The string to check.
     * @return boolean        true if it does, false if it doesn't.
     */
    function is8bit($string)
    {
        if (is_string($string)) {
            for ($i = 0; $i < strlen($string); $i++) {
                if (ord($string[$i]) >> 7)
                    return true;
            }
            return false;
        }
        return false;
    }
    
    /**
     * Encode a string containing non-ascii characters according to RFC 2047.
     *
     * @param string $text    The text to encode.
     * @param string $charset (optional) The character set of the text.
     * @param boolean $outer  Is this the final iteration?
     *
     * @return string The text, encoded only if it contains non-ascii characters.
     */
    function encode($text, $charset = null, $outer = true)
    {
        if (MIME::is8bit($text)) {
            if (((strlen($text) * 3) + strlen($charset) + 7) > 76) {
                $text = MIME::encode(substr($text, 0, (23 - strlen($charset))), $charset) . MIME::encode(substr($text, (23 - strlen($charset))), $charset, false);
            } else {
                $text = "=?$charset?B?" . strtr(trim(base64_encode($text)), ' ', '_') . "?=\n\t";
            }
        }
        
        // if this is the final iteration, take off any trailing
        // newline/tab chars.
        if ($outer && (substr($text, -2) == "\n\t"))
            $text = substr($text, 0, -2);
        
        return $text;
    }
    
    /**
     * Encode a string containing email addresses according to RFC 2047.
     *
     * This differs from MIME::encode() because it keeps email
     * addresses legal, only encoding the personal information.
     *
     * @param string $text      The email addresses to encode.
     * @param string $charset   (optional) The character set of the text.
     * @param string $defserver (optional) The default domain to append to mailboxes.
     *
     * @return string The text, encoded only if it contains non-ascii characters.
     */
    function encodeAddress($text, $charset = null, $defserver = null)
    {
        include_once 'Mail/RFC822.php';
        
        $addr_arr = Mail_RFC822::parseAddressList($text, $defserver, false, false);
        $text = '';
        if (is_array($addr_arr)) {
            foreach ($addr_arr as $addr) {
                if (empty($addr->personal)) {
                    $personal = '';
                } else {
                    if ((substr($addr->personal, 0, 1) == '"') &&
                        (substr($addr->personal, -1) == '"')) {
                    &nb

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【论坛讨论

   相关文章:
·将OICQ数据转成MYSQL数据 ·用PHP实现ODBC数据分页显示一例
·php生成WAP页面 ·PHP与Javascript的两种交互方式
·PHP+Javascript模拟Matrix画面 ·WHOIS类的修改版

   文章评论:(条)
  
 请留名: 匿名评论   点击查看所有评论 网管论坛
 

  责任编辑:一分  声明:刊登此文章是为了传递更多信息,文章内容仅供参考,转载请注明出处。