Remove all kind of quotation marks

I say: “Remove all quotation marks from a string“. You say: “stripslashes“. Works fine. In most of situations.

If you have to deal with quotation marks manually entered to rich editor (i.e. by copying large block of text from Word for example) or with a string being a poor effect of even more poor encoding routine, you may be sure, that stripslashes won’t do all the dirty work for you.

This is the place where my handy method comes in.

I used, old good str_replace function in not so known manner of feeding it with an array:

/**
 * Returns string purged out of all kind of quotation marks.
 *
 * @param  string $str Input string to be purged.
 * 
 * @return string      Resulting string, purged out of all kind of quotation marks.
 */
public static function removeAllKindOfQuotationMarks($str)
{
    return str_replace(array
    (
        '"', "'",
        '‘', '‘', '‘', '‘',
        '’', '’', '’', '’',
        '“', '“', '“', '“',
        '”', '”', '”', '”',
        '‚', '‚', '‚', '‚',
        '„', '„', '„', '„',
        '′', '′', '′', '′',
        '″', '″', '″', '″'
    ), '', $str);
}

And there you go. This beauty here should be able to deal with nearly any string, on which stripslashes dies. Replacement table comes from this source, partially altered and enhanced by me.

2 comments on “Remove all kind of quotation marks

    1. admin

      Some more details, please? How it is not working?

      Any example what you’re getting vs. what is expected to get?

Leave a Reply