How to remove double quotes from a string

How to remove double quotes from a string

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

Stefan Bogdanescu

Founder & Senior Architect

Stripping Delimiters from Strings: A Practical Guide

When working with string manipulation, developers often encounter data that includes unwanted delimiters. Consider a scenario where your input variable $a is defined as '"some text'. The goal is to transform this into some text, effectively removing the leading double quote while preserving the internal content. While this specific case involves stripping only the start of the string, the underlying technique applies broadly to handling quoted values in configuration files, log parsing, and API responses.

Language-Specific Approaches

The most direct solution depends entirely on the programming language you are using, as each has its own set of built-in functions for pattern matching and substring extraction.

In PHP, where the source example originates, you can use str_replace if you are certain the quote appears only once at the beginning. However, relying solely on replacement is brittle. If your string might contain quotes elsewhere (e.g., "some text with "quotes""), a simple str_replace('"', '', $a) would corrupt the data by removing every occurrence.

A more robust approach in PHP involves checking the first character and slicing the string accordingly:

$a = '"some text';
if (strlen($a) > 0 && $a[0] === '"') {
    $a = substr($a, 1);
}
// Result: 'some text'

This logic explicitly validates that the first character is a double quote before removing it. It prevents index-out-of-bounds errors on empty strings and ensures you don't accidentally strip a valid opening quote from a properly formatted string like "Hello".

Regular Expressions for Flexibility

If your requirement is less about "stripping the first character if it's a quote" and more about "removing quotes that wrap text," regular expressions provide a powerful alternative. This is common when parsing unstructured logs or extracting values from JSON-like fragments without a full parser.

You can use preg_match or preg_replace to target only leading and trailing quotes:

$a = '"some text"';
// Remove leading and trailing quotes if they exist
$a = preg_replace('/^"/|"$/', '', $a);
// Result: 'some text'

This pattern /^"/|"$/' matches a double quote at the very start of the string (^") or at the very end ("$). This approach is significantly safer than replacing all occurrences because it anchors the match to the boundaries. It handles cases like "text" gracefully, leaving internal quotes intact.

Handling Escaped Characters

One critical pitfall in string manipulation is escaped characters. In many languages, a double quote inside a string literal is represented as \". When parsing raw data, you must distinguish between an actual quote character and an escape sequence.

If your input string is actually "some \"text\"", simply removing the surrounding quotes based on visual inspection will fail. You need to ensure you are operating on the raw byte representation or a parsed token stream, not just a human-readable representation. In PHP, if you received the string from user input or an API that uses JSON encoding, the backslashes would already be processed depending on how you read the file or network stream. Always verify whether your source data is "escaped" (stored as \") or literal (stored as ").

Trade-offs and Performance Considerations

Choosing between a simple substring operation and a regex solution involves trade-offs. The substr method is O(1) in terms of pattern matching overhead; it just reads the first byte. This makes it faster for high-throughput scenarios where you know the schema strictly. Regex introduces parsing overhead, which becomes relevant only if you are processing millions of rows in memory or dealing with complex patterns that require lookaheads.

However, flexibility is rarely free. If your data source is unreliable and occasionally provides strings without quotes when they should have them, a conditional substr block handles both cases elegantly: it does nothing if the first character isn't a quote, and strips it otherwise. This idempotency is often more valuable than raw speed in production environments.

For developers working within the Laravel ecosystem, you might find yourself handling this logic inside service classes or Eloquent scopes. While Laravel provides robust tools for JSON manipulation via json_decode, dealing with plain strings requires careful attention to these edge cases. If you need a reusable utility for cleaning up string literals from various sources, consider encapsulating this logic in a helper function within your App namespace to keep your controllers clean.

Ultimately, the choice between slicing and regex depends on whether you are cleaning a known format or parsing unknown text. Understanding the nature of your input data—specifically regarding escaping and boundary conditions—is the key to writing maintainable string manipulation code.

Our network