PHP 如何从多行中删除 =20

PHP How to remove =20 from multiple lines

我想知道是否可以从一个文本块中的多行末尾仅删除 =20?这是我到票务网关的电子邮件。当收到回复并包含多个“=20”时,我想删除它们。但是,如果 =20 是电子邮件的合法部分(例如 URL),我希望它保持原样。传入电子邮件的示例:

$correspondence = <<<EOF
Hello=20
=20
Thank you for getting back to me.=20
The link you need is http://domain.com/index.php?id=204726 .=20
Regards=20
EOF;

您可以使用带有正则表达式的丢弃技术来做到这一点:

http://.*=20$(*SKIP)(*FAIL)|=20

Working demo

这个正则表达式背后的想法是丢弃匹配 (*SKIP)(*FAIL) 的内容并保留 =20。因此,对于上述情况,正则表达式将丢弃链接。

您可以在 Substitution 部分看到预期的输出。

php代码:

$re = "/http.*=20$(*SKIP)(*FAIL)|=20/m"; 
$str = "$correspondence = <<<EOF\nHello=20\n=20\nThank you for getting back to me.=20\nThe link you need is http://domain.com/index.php?id=204726 .=20\nRegards=20\nEOF;"; 
$subst = ""; 

$result = preg_replace($re, $subst, $str);

这是quoted-printable encoding, which uses = followed by hex codes to encode special characters in plain text. Use quoted_printable_decode解码以这种方式编码的消息。您不必担心文本中任何地方的文字 =20,因为它应该被编码为 =3D20=3D= 符号的编码),并且解码后你会得到原来的 =20