How do I remove quotes from a string?

How do I remove quotes from a string?

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

Stefan Bogdanescu

Founder & Senior Architect

How do I remove quotes from a string?

When working with user input or data scraped from external sources, strings often arrive wrapped in unwanted quotation marks. Whether these are double quotes ("), single quotes ('), curly quotes (", "), or even language-specific Unicode variants, the presence of these characters can break parsers, corrupt database indexes, or fail strict format validators.

Consider a scenario where you receive a payload like $string = "my text has \"double quotes\" and 'single quotes'". If your downstream logic expects raw content without delimiters, stripping these characters becomes a necessary preprocessing step. The approach depends entirely on whether the quotes are literal characters in the string or structural syntax elements that need interpretation.

Literal Character Removal via Regex

If your goal is to strip specific quote characters regardless of context, regular expressions offer the most flexible solution. This method targets the actual Unicode code points for quotation marks.

In PHP, you can use preg_replace to remove double quotes and single quotes simultaneously:

$string = "my text has \"double quotes\" and 'single quotes'";

// Remove standard double and single quotes
$cleaned = preg_replace('/["\x27]/', '', $string);

echo $cleaned; 
// Output: my text has  double quotes  and  single quotes

Here, " represents a standard double quote, while \x27 is the hexadecimal escape for a single quote. This pattern also handles the curly double quotes (\u201C, \u201D) if you expand the character class:

$cleaned = preg_replace('/["\x27\x201C\x201D]/', '', $string);

This approach is robust because it treats quotes as data. It does not care where they appear in the sentence; it simply removes every instance found matching the pattern. Be cautious, however: if your string contains escaped quotes (like \"), this method will remove both the backslash and the quote unless you specifically target only the quote character itself without affecting the escape sequence logic of your parser.

Handling Escaped Sequences

A common pitfall occurs when the input string contains escape sequences. In the example $string = "my text has \"double quotes\"...", the \" inside a double-quoted PHP variable represents a literal backslash followed by a quote character in memory, or just a quote if the parser interprets it as an escaped string delimiter.

If you are processing raw JSON or CSV where quotes are used to wrap fields containing commas, blindly removing all quotes can destroy the data structure. For instance, "He said \"Hello\"", becomes He said Hello if you strip every quote. This might be desirable for display but fatal for parsing.

You must distinguish between quotes acting as delimiters and quotes acting as content. If your application layer handles JSON or CSV parsing natively (using json_decode or similar), let that parser handle the delimiter logic before applying string cleaning. Only remove quotes if you have confirmed they are part of the textual content and not structural syntax.

Why Not Simple String Replacement?

While str_replace works for single character types, it lacks the flexibility to handle multiple quote styles in one pass without verbose chaining. More importantly, regex allows you to define negative lookaheads if you need to preserve quotes that are escaped but remove those that are not.

// This is complex and often unnecessary unless dealing with specific escape logic
$cleaned = preg_replace('/(?<!\\)["\x27\x201C\x201D]/', '', $string);

The (?<!\\) ensures we only remove a quote if it isn't preceded by a backslash. This is a critical trade-off: removing quotes blindly simplifies the string but risks data loss; preserving escape-awareness adds complexity but maintains integrity. Choose based on whether the downstream process expects raw text or structured syntax.

When to Use LaravelCompany Resources

If you are building an application within the Laravel ecosystem and need to sanitize input strings extensively, consider leveraging community packages that handle complex encoding issues automatically. For instance, if you find yourself repeatedly cleaning user-generated content across multiple controllers, a dedicated package might offer configuration options for handling various quote types without writing custom regex every time. You can explore solutions at https://laravelcompany.com which often provides curated resources for common Laravel data sanitization tasks. However, always verify that any third-party package handles Unicode edge cases correctly before integrating it into production code.

Final Considerations

Before executing a mass removal operation, validate the output against your expected schema. If you are cleaning strings intended for SQL injection prevention or XSS mitigation, remember that removing quotes alone does not secure the application; parameterized queries and output escaping remain mandatory. The removal of quotes is purely cosmetic or structural relative to security vulnerabilities unless those quotes were facilitating a syntax-based exploit, which is rare in modern frameworks.

Ultimately, the decision to strip quotes hinges on whether they are noise or signal. If they are noise, regex provides a precise tool. If they are signal, let your parser handle them.

Our network