Craig's Scrapbook - Home




Removing slashes from form mailer scripts
The Problem
Many simple PHP scripts that you might use for, say, a contact form insert an annoying backward slash wherever an apostrophe appears. If someone writes, I'd say so - that'd be great, you would receive the message, I\'d say so - that\'d be great. When you reply to the message you've got to go through and tediously remove the slashes or risk causing your correspondent a cruel bout of soul-searching over their typing skills.

A Solution
If you aren't scared of editing a line of code (it's easy - I'm an idiot and even I can do it!) follow these steps:

  1. Open a copy of the PHP file that you use for processing and mailing your forms in a plain text editor like Notepad or a dedicated code editor. You will see a bunch of PHP gobbledygook.
  2. Locate the line (probably towards the end) that starts thus: mail(
    If you can't find this line, you're out of luck as far as my knowledge goes, but you can always grab another script that already incorporates the below into its code, for example see here.
  3. Copy and paste the entire line starting with mail( below itself into a new line (to keep in case you mess things up and need to revert to the original).
  4. Deactivate one of the lines by putting two forward slashes out the front of it (see example below). This turns it into a line of code that PHP treats as a programmer's comment—to be ignored by the server.
  5. Within the active line, find the bit that serves as a code function for your email message body as opposed to the subject line or recipient. It will probably be something like $message or $body.
  6. Enclose that function within the stripslashes function, for example: stripslashes($messagebody)
  7. Save and upload this modified PHP file to your server, then test it. If worst comes to worst and you can't fiddle around with it to get it to work, revert to the original line of code and learn to live with slashed-up messages!

stripslashes example


Copyright © Craig Forsythe, 2013. All rights reserved. Contact.