本文介绍了eregi_replace("[\]",'',$data) -- 这行有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的 phpmailer 示例文件中,有以下一行:

In the latest phpmailer example file, there's the following line:

$body = eregi_replace("[\]",'',$body);

由于我不是很擅长正则表达式,所以我无法弄清楚上面的内容是什么以及我在编写自己的数据块时是否需要使用它($body).谁能帮我解决这个问题?

As I'm not really good in regular expressions, I can't figure out what the above does and whether I need to use it when I write my own block of data ($body). Could anyone help me figure this out?

编辑

我真的正确地复制了它.这是原始 phpmailer 示例文件中的一整块代码,完全没有改动:

I really copied it properly. Here is a whole chunk of code from the original phpmailer example file, completely untouched:

require_once('../class.phpmailer.php');

$mail             = new PHPMailer(); // defaults to using php "mail()"

$body             = file_get_contents('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->AddReplyTo("[email protected]","First Last");

$mail->SetFrom('[email protected]', 'First Last');

推荐答案

该代码正在删除 $body 中的所有反斜杠.

That code is removing all backslashes from $body.

虽然乍一看可能有点奇怪,但正则表达式是正确的.当反斜杠位于 POSIX 正则表达式中的方括号内时,它不是元字符.

Though it may look a little odd at first glance, the regex is correct. The backslash isn't a metacharacter when it's inside brackets in a POSIX regex.

无论如何,这段代码有各种各样的问题,特别是因为它应该是一个例子:

There's all sorts of problems with this code anyway, though, especially since it's supposed to be an example:

  • 它使用已弃用的 ereg(或 POSIX)正则表达式函数系列之一.最近的 PHP 示例几乎都应该使用 preg(与 Perl 兼容)系列.
  • 它使用不区分大小写的匹配(eregi 中的 i),即使它不匹配任何字母,所以大小写无关.
  • 最重要的是,更换的实际目的尚不清楚.我只能猜测这是对 PHP 的 magicquotes 功能的误导性尝试它会自动为各种事物添加反斜杠.

  • It uses one of the deprecated ereg (or POSIX) family of regex functions. Half-recent PHP examples should pretty much all be using the preg (Perl-compatible) family instead.
  • It uses case-insensitive matching (the i in eregi) even though it's not matching against any letters, so case is irrelevant.
  • Most importantly, the actual purpose of the replacement is unclear. I can only guess that this is a misguided attempt to account for PHP's magic quotes feature that automatically adds backslashes to all sorts of things.

需要明确的是,这段代码不是处理魔术引号的正确方法,因为它会从$body中删除所有反斜杠,即使是原始输入中存在的真实".stripslashes() 函数正是为此而设计的用例.或者,由于示例处理从文件中读取,您可以简单地 关闭魔术引号.

To be clear, this code is not a proper way to deal with magic quotes, since it will remove all backslashes from $body, even "real" ones present in the original input. The stripslashes() function is intended for exactly this use case. Or, since the example deals with reading from a file, you could simply turn off magic quotes.

这篇关于eregi_replace("[\]",'',$data) -- 这行有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 07:10