How to remove single and double quotes from a string

How to remove single and double quotes from a string

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

How to remove single and double quotes from a string

Attempting to strip quotation marks from a string often leads to unexpected behavior when using overly aggressive regular expressions or invalid character classes. The core issue usually stems from misunderstanding how regex engines interpret specific symbols within delimiters or how they handle "unknown modifiers." When you see outputs like quot appearing in your result instead of the expected empty string, or encounter warnings about unknown modifiers, it is a clear sign that the pattern syntax is flawed.

Consider a function designed to sanitize input by removing everything that isn't alphanumeric:

function string_sanitize($s) {
    $result = preg_replace("/[^a-zA-Z0-9]+/", "", $s);
    return $result;
}

While the intention here is good, this approach has a critical flaw: it removes all characters that are not letters or numbers. This includes spaces, punctuation marks like periods or commas, and crucially, apostrophes and quotes. However, the specific behavior described in recent discussions—where double quotes turn into quot—suggests an external library is performing entity decoding before the regex runs, or the input stream is already XML-encoded. If your source material shows This is 'the' first example returning Thisis030the039firstexample, that 039 is the decimal code for a single quote, and quot suggests an entity like ".

If you are running this in a Laravel environment or dealing with HTML entities, simply removing non-alphanumerics might not be enough. You need to explicitly target the quote characters before they get mangled by other processes. The safest way to remove single (') and double (") quotes without accidentally stripping valid content is to use a specific pattern that targets only those characters.

Here is a more precise implementation:

function strip_quotes($s) {
    // Remove both single and double quotes explicitly
    return preg_replace("/['\"]/", "", $s);
}

This pattern ['\"] creates a character class containing a single quote, a backslash (to escape the double quote inside the brackets), and an end anchor if needed. By matching only these two characters, you preserve spaces and other punctuation that might be semantically important in your data. For instance, removing quotes from "Hello World" yields Hello World, whereas a broad removal of non-alphanumerics would also delete the space, resulting in HelloWorld.

Handling HTML Entities

If your input contains encoded entities like " or ', the regex above won't catch them. You must decode the string first. In PHP, html_entity_decode() handles this conversion to standard characters before you apply your sanitization logic.

function clean_input($s) {
    // Decode entities first
    $decoded = html_entity_decode($s);
    // Then strip the actual quote characters
    return preg_replace("/['\"]/", "", $decoded);
}

This two-step process ensures that whether your data comes as raw text or pre-encoded HTML, the quotes are consistently removed. The order of operations is vital here; decoding first prevents regex errors where the engine tries to match a pattern against a literal string representation of an entity.

Trade-offs and Pitfalls

When stripping quotes, you must decide if you want to preserve apostrophes used in contractions (e.g., it's, don't). The previous examples treated all quotes as delimiters to be removed. If your text is natural language, blindly removing every ' might break words like don't.

// This removes ALL quote-like characters indiscriminately
preg_replace("/['\"]/", "", "It's a test"); // Returns: Its a test

// A more nuanced approach might only remove double quotes if they act as delimiters
// or use negative lookarounds if you need to preserve internal apostrophes.

In many data processing scenarios, however, the goal is simply to normalize the string for storage or display where punctuation is noise. If your source data uses single quotes exclusively for ownership (e.g., O'Reilly) and you want to keep them, but remove double quotes used for strings, you need to differentiate based on context or use a whitelist approach.

Always validate your output against known bad patterns. If you are seeing 039 in your results, ensure you aren't accidentally concatenating ASCII codes into your string logic. The regex /[^a-zA-Z0-9]+/ is often too destructive because it treats spaces as invalid characters to be removed. Unless you specifically want to collapse all whitespace, prefer patterns that target the specific unwanted characters rather than a broad "everything else" filter.

Our network