Remove parentheses and the content in between

Remove parentheses and the content in between

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

Stefan Bogdanescu

Founder & Senior Architect

Stripping Parenthetical Content in PHP Strings

Handling text patterns like ABC (Test1) often requires more than a simple string replacement when the content between the delimiters varies. If your goal is to remove the parentheses and everything inside them—leaving only ABC—you need a strategy that accounts for variable lengths and potential edge cases. A naive approach using str_replace or substr works only if you know the exact length of the content to strip, which defeats the purpose of pattern matching.

The most robust solution utilizes regular expressions via preg_replace. This function allows you to define a capture group for the text inside the parentheses and instruct the regex engine to discard it during the replacement process.

Consider the input string:

$input = 'ABC (Test1)';

To achieve the desired output of ABC, you can use the following pattern:

$result = preg_replace('/\((.*?)\)/', '', $input);
var_dump($result); // Outputs: string(3) "ABC"

Here, \( and \) match the literal parentheses. The (.*?) portion is a capturing group that uses a non-greedy quantifier (.*?) to match any character sequence between the opening and closing parenthesis. By replacing this entire matched segment with an empty string '', you effectively remove both the delimiters and their contents.

Handling Nested or Multiple Instances

A common pitfall in text processing is assuming that all instances of a pattern need removal, or conversely, needing to preserve specific ones. The regex approach scales well here. If your input contains multiple parenthetical sections, such as Project Alpha (Phase 1) and Project Beta (Phase 2), the same single-pass preg_replace call will handle both occurrences simultaneously:

$input = 'Project Alpha (Phase 1) and Project Beta (Phase 2)';
$result = preg_replace('/\((.*?)\)/', '', $input);
// Result: "Project Alpha  and Project Beta "

Note the extra spaces. Since the pattern removes the parentheses but leaves the surrounding text intact, you often end up with double spaces where the parenthetical content used to be. To clean this up, you might chain a second replacement or use a callback function to handle whitespace normalization automatically. For instance, replacing \(\s*(.*?)\s*\) captures optional whitespace inside the parentheses and outside them, ensuring cleaner results:

$result = preg_replace('/\(\s*(.*?)\s*\)/', '', $input);
// Result: "Project Alpha  and Project Beta" (with double spaces)

If you want to collapse those double spaces into single ones, a secondary regex like preg_replace('/\s+/', ' ', $result) can handle it. However, be cautious with this global cleanup if the original text legitimately contains multiple spaces that should not be collapsed.

Performance and Readability Considerations

While preg_replace is powerful, it introduces regex parsing overhead compared to simple string functions. If you are processing millions of rows in a high-throughput loop where the pattern (.*?) is guaranteed to exist at a known position, a manual substring approach might yield better performance. However, for general-purpose text cleaning or user-generated content, the flexibility of regex far outweighs the minor cost.

Another consideration is escaping. If your input string contains backslashes before the parentheses, like ABC \(Test\1, you must account for that in your pattern. The simple \( handles standard cases, but complex inputs require careful analysis to avoid false negatives where valid data gets stripped incorrectly.

For developers working within the Laravel ecosystem, this logic can be easily encapsulated in a helper method or chained directly within a form request validation rule if dealing with model attributes. While specific libraries exist for complex text normalization, often a well-crafted regex is the most lightweight dependency-free solution available.

Our network