Getting rid of escaped ' in email

Posted on 2011-04-21
Last Modified: 2012-05-11
I have a form with a textarea input to let the customer enter comments. The PHP code  that processes the form puts the string from the textarea into the body of an email and sends it to me.

The problem is that if the client enters an apostrophe ( ' ), as in "Here's a new order"  it shows up in the email I get as " Here\'s a new order."

Does anyone know how I can get rid of the \  in the email?

Thanks for any help.
Question by:steva
    LVL 2

    Expert Comment

    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    You can get rid of any char by using str_replace():
    $char = "'";
    $str = str_replace($char, '', $str);

    Open in new window

    LVL 12

    Expert Comment

    by:Mohamed Abowarda
    You can also try to double the quotation by using the following code:
    $str = str_replace("'", "''", $str);

    Open in new window

    LVL 107

    Accepted Solution

    Your PHP installation has magic quotes turned on.  You do not want that.  Please see the man page here:

    So you can use the stripslashes() function or you can do the recommended thing and stop using the deprecated "feature" -- I would opt for the latter.  Magic Quotes was an early attempt by the PHP authors to make the language so easy to use that anyone, even someone with no understanding of computer science or data base requirements, could write PHP programs.  Along with "register globals" it has turned out to be a poor choice for a variety of reasons.

    After you turn off magic quotes, you will want to be certain to use the appropriate escape mechanisms to prepare data for use in queries, like mysql_real_escape_string()

    Author Closing Comment


    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now