使 Quoted_printable_encode() 在 PHP4 上工作

发布于 2024-10-06 02:49:45 字数 281 浏览 7 评论 0原文

我正在尝试使用 quoted_printable_encode()和 [quoted_prinatble_decode(),但问题是我的服务器运行的是 PHP4,并且根据 PHP 文档,quoted_printable_encode() 仅在 PHP 5 >= 5.3.0 上可用。有谁知道黑客或解决方法,以便我可以利用编码功能

I'm trying to use the quoted_printable_encode() and [quoted_prinatble_decode(), but the problem is my server is running PHP4, and according to the PHP docs, quoted_printable_encode() is only available on PHP 5 >= 5.3.0. Does anyone know of a hack or workaround so I could utilize the encode func

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

爱情眠于流年 2024-10-13 02:49:45

根据文档,您可以使用 imap_8bit,但是您应该真的考虑升级到php5。它已经存在了 6 年多了!

According to the docs you could use imap_8bit, however you should really consider upgrading to php5. It's been around for over 6 years!

堇色安年 2024-10-13 02:49:45

我对大家的支持和帮助感激不尽。我完全同意我们应该升级到 PHP5 - 但这是针对我工作的大公司的,正如你们许多人所知,有些事情应该发生,但不发生或不能发生。无论如何,我只是一名实习生,所以我会按照他们说的去做 =)

我想通了 -

function quoted_printable_encode($input, $line_max = 75) {                  // Quoted_printable_encode that works with php 4.x
   $hex = array('0','1','2','3','4','5','6','7', 
                          '8','9','A','B','C','D','E','F'); 
   $lines = preg_split("/(?:\r\n|\r|\n)/", $input); 
   $linebreak = "=0D=0A=\r\n"; 
   /* the linebreak also counts as characters in the mime_qp_long_line 
    * rule of spam-assassin */ 
   $line_max = $line_max - strlen($linebreak); 
   $escape = "="; 
   $output = ""; 
   $cur_conv_line = ""; 
   $length = 0; 
   $whitespace_pos = 0; 
   $addtl_chars = 0; 

   // iterate lines 
   for ($j=0; $j<count($lines); $j++) { 
     $line = $lines[$j]; 
     $linlen = strlen($line); 

     // iterate chars 
     for ($i = 0; $i < $linlen; $i++) { 
       $c = substr($line, $i, 1); 
       $dec = ord($c); 

       $length++; 

       if ($dec == 32) { 
          // space occurring at end of line, need to encode 
          if (($i == ($linlen - 1))) { 
             $c = "=20"; 
             $length += 2; 
          } 

          $addtl_chars = 0; 
          $whitespace_pos = $i; 
       } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { 
          $h2 = floor($dec/16); $h1 = floor($dec%16); 
          $c = $escape . $hex["$h2"] . $hex["$h1"]; 
          $length += 2; 
          $addtl_chars += 2; 
       } 

       // length for wordwrap exceeded, get a newline into the text 
       if ($length >= $line_max) { 
         $cur_conv_line .= $c; 

         // read only up to the whitespace for the current line 
         $whitesp_diff = $i - $whitespace_pos + $addtl_chars; 

        /* the text after the whitespace will have to be read 
         * again ( + any additional characters that came into 
         * existence as a result of the encoding process after the whitespace) 
         * 
         * Also, do not start at 0, if there was *no* whitespace in 
         * the whole line */ 
         if (($i + $addtl_chars) > $whitesp_diff) { 
            $output .= substr($cur_conv_line, 0, (strlen($cur_conv_line) - 
                           $whitesp_diff)) . $linebreak; 
            $i =  $i - $whitesp_diff + $addtl_chars; 
          } else { 
            $output .= $cur_conv_line . $linebreak; 
          } 

        $cur_conv_line = ""; 
        $length = 0; 
        $whitespace_pos = 0; 
      } else { 
        // length for wordwrap not reached, continue reading 
        $cur_conv_line .= $c; 
      } 
    } // end of for 

    $length = 0; 
    $whitespace_pos = 0; 
    $output .= $cur_conv_line; 
    $cur_conv_line = ""; 

    if ($j<=count($lines)-1) { 
      $output .= $linebreak; 
    } 
  } // end for 

  return trim($output); 
} // end quoted_printable_encode 

让它发挥作用。我碰巧在你们发帖后回来查看,所以不幸的是我没有测试你们的解决方案,但上面的功能运行得非常完美!

再次感谢大家!

I can't thank you all enough for the support and help. I totally agree we should upgrade to PHP5 - but this is for the large corporation I work for, and as many of you know, there are certain things that SHOULD happen, but don't or can't. I'm just an intern, anyways, so I'm going to do as they say =)

I figured it out -

function quoted_printable_encode($input, $line_max = 75) {                  // Quoted_printable_encode that works with php 4.x
   $hex = array('0','1','2','3','4','5','6','7', 
                          '8','9','A','B','C','D','E','F'); 
   $lines = preg_split("/(?:\r\n|\r|\n)/", $input); 
   $linebreak = "=0D=0A=\r\n"; 
   /* the linebreak also counts as characters in the mime_qp_long_line 
    * rule of spam-assassin */ 
   $line_max = $line_max - strlen($linebreak); 
   $escape = "="; 
   $output = ""; 
   $cur_conv_line = ""; 
   $length = 0; 
   $whitespace_pos = 0; 
   $addtl_chars = 0; 

   // iterate lines 
   for ($j=0; $j<count($lines); $j++) { 
     $line = $lines[$j]; 
     $linlen = strlen($line); 

     // iterate chars 
     for ($i = 0; $i < $linlen; $i++) { 
       $c = substr($line, $i, 1); 
       $dec = ord($c); 

       $length++; 

       if ($dec == 32) { 
          // space occurring at end of line, need to encode 
          if (($i == ($linlen - 1))) { 
             $c = "=20"; 
             $length += 2; 
          } 

          $addtl_chars = 0; 
          $whitespace_pos = $i; 
       } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { 
          $h2 = floor($dec/16); $h1 = floor($dec%16); 
          $c = $escape . $hex["$h2"] . $hex["$h1"]; 
          $length += 2; 
          $addtl_chars += 2; 
       } 

       // length for wordwrap exceeded, get a newline into the text 
       if ($length >= $line_max) { 
         $cur_conv_line .= $c; 

         // read only up to the whitespace for the current line 
         $whitesp_diff = $i - $whitespace_pos + $addtl_chars; 

        /* the text after the whitespace will have to be read 
         * again ( + any additional characters that came into 
         * existence as a result of the encoding process after the whitespace) 
         * 
         * Also, do not start at 0, if there was *no* whitespace in 
         * the whole line */ 
         if (($i + $addtl_chars) > $whitesp_diff) { 
            $output .= substr($cur_conv_line, 0, (strlen($cur_conv_line) - 
                           $whitesp_diff)) . $linebreak; 
            $i =  $i - $whitesp_diff + $addtl_chars; 
          } else { 
            $output .= $cur_conv_line . $linebreak; 
          } 

        $cur_conv_line = ""; 
        $length = 0; 
        $whitespace_pos = 0; 
      } else { 
        // length for wordwrap not reached, continue reading 
        $cur_conv_line .= $c; 
      } 
    } // end of for 

    $length = 0; 
    $whitespace_pos = 0; 
    $output .= $cur_conv_line; 
    $cur_conv_line = ""; 

    if ($j<=count($lines)-1) { 
      $output .= $linebreak; 
    } 
  } // end for 

  return trim($output); 
} // end quoted_printable_encode 

Makes it work. I happened to check back here after you all posted, so unfortunately I have not tested your solutions, but the function above works just perfectly!

Thank you all again!

回忆凄美了谁 2024-10-13 02:49:45

您是否安装了 IMAP 模块?使用 imap_8bit() 来代替怎么样?

Do you have the IMAP module installed? What about using imap_8bit() instead?

萌︼了一个春 2024-10-13 02:49:45

最好的办法是升级到 PHP 5.4
死了

如果这真的、真的不是一个选项,手册页您链接到有一些替代方案(imap_8bit( ) 以及用户贡献的注释中的一些替代实现)。

The best thing to do would be upgrade to PHP 5. 4 is
dead.

If that is really, really not an option, the manual page you link to has a few alternatives (imap_8bit() and some alternative implementations in the user contributed notes).

攒一口袋星星 2024-10-13 02:49:45
    public function quoted_printable($mesg){
  $orders = unpack("C*", $mesg);
  unset($mesg);
  array_filter($orders, array($this, 'cb_qp'));
  return implode($orders);
}

// Quoted-Printable Callback
private function cb_qp(&$byte){
  $byte = ($byte > 126 || $byte == 61 || $byte == 37) ? sprintf('=%X', $byte) : pack("C", $byte);
}
 // http://rolfrost.de/proglog.html?d=20130324
    public function quoted_printable($mesg){
  $orders = unpack("C*", $mesg);
  unset($mesg);
  array_filter($orders, array($this, 'cb_qp'));
  return implode($orders);
}

// Quoted-Printable Callback
private function cb_qp(&$byte){
  $byte = ($byte > 126 || $byte == 61 || $byte == 37) ? sprintf('=%X', $byte) : pack("C", $byte);
}
 // http://rolfrost.de/proglog.html?d=20130324
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文