不推荐使用:preg_replace():在phpmailer中不推荐使用/ e修饰符

时间:2014-07-24 06:02:53

标签: php phpmailer

我正在使用class.phpmailer.php从我的本地服务器发送电子邮件,它在我的本地服务器上使用PHP版本php5.3.4处理得很好但在我将PHP版本更新为5.5.4后,它显示以下内容消息:

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in class.phpmailer.php`

这是导致错误的行:

$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e', "'='.sprintf('%02X', ord(stripslashes('\\1')))", $encoded);

2 个答案:

答案 0 :(得分:7)

尝试并替换:

$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',"'='.sprintf('%02X', ord(stripslashes('\\1')))", $encoded);

使用

$encoded = preg_replace_callback('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/',function($m) { return '='.sprintf('%02X', ord(stripslashes($m[1]))); }, $encoded);

答案 1 :(得分:0)

switch (strtolower($position)) {
  case 'phrase':
    //$encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
    $encoded = preg_replace_callback("/([^A-Za-z0-9!*+\/ -])/e",function($m) { return '='.sprintf('%02X', ord(stripslashes($m[1]))); }, $encoded);
    break;
  case 'comment':
    //$encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
    $encoded = preg_replace_callback("/([\(\)\"])/e",function($m) { return '='.sprintf('%02X', ord(stripslashes($m[1]))); }, $encoded);
  case 'text':
  default:
    // Replace every high ascii, control =, ? and _ characters
    //TODO using /e (equivalent to eval()) is probably not a good idea
    //$encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',"'='.sprintf('%02X', ord('\\1'))", $encoded);
    $encoded = preg_replace_callback('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/',function($m) { return '='.sprintf('%02X', ord(stripslashes($m[1]))); }, $encoded);
    break;
}

是的。它适用于php 7.2。