In the latest phpmailer example file, there’s the following line:
$body = eregi_replace("[\]",'',$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?
EDIT
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("name@yourdomain.com","First Last");
$mail->SetFrom('name@yourdomain.com', 'First Last');
That code is removing all backslashes from
$body.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(or POSIX) family of regex functions. Half-recent PHP examples should pretty much all be using thepreg(Perl-compatible) family instead.iineregi) 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.
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. Thestripslashes()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.