Remove non-numeric characters (excluding periods and commas) from a string (i.e. remove all characters except numbers, commas, and periods)

Remove non-numeric characters (excluding periods and commas) from a string (i.e. remove all characters except numbers, commas, and periods)

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

Stefan Bogdanescu

Founder & Senior Architect

Cleaning Numeric Strings While Preserving Delimiters

When working with financial data, scientific notation, or legacy datasets imported from CSV files, you often encounter strings that mix digits with unwanted noise—letters, spaces, and symbols that break parsing logic. You need to sanitize these inputs without stripping the structural characters that define the number's magnitude or decimal point. Specifically, you want to keep periods (.) and commas (,) intact while removing everything else.

In PHP, a common scenario involves transforming values like AR3,373.31 into 3,373.31 or converting 12.322,11T into 12.322,11. The goal is to extract the numeric skeleton of the string while preserving its formatting conventions.

The Regex Approach

The most efficient way to solve this in PHP is using regular expressions with the preg_replace function. You can construct a pattern that explicitly matches and removes any character that is not a digit, comma, or period.

Here is how you would implement this:

$input = 'AR3,373.31';
$output = preg_replace('/[^0-9.,]/', '', $input);

// Result: "3,373.31"

The core of the logic lies in the character class [^0-9.,]. The caret symbol (^) at the start of the brackets negates the set, telling the regex engine to match anything except digits (0-9), periods, and commas. These excluded characters are then replaced with an empty string.

If you are handling multiple variables or processing a dataset, you can wrap this in a loop or apply it to an array using array_map:

$data = ['AR3,373.31', '12.322,11T'];
$cleaned = array_map(fn($str) => preg_replace('/[^0-9.,]/', '', $str), $data);

// Output: ["3,373.31", "12.322,11"]

Handling Edge Cases and Delimiters

While the regex solution is concise, it operates under specific assumptions about how numbers are formatted. The pattern above treats commas and periods strictly as literal characters to be preserved. However, in international contexts, these symbols serve different roles. A comma often acts as a decimal separator in European locales (e.g., 12.345,67), whereas a period is the standard decimal point in US English.

If your source data relies on locale-specific formatting where commas denote decimals, blindly preserving them might lead to incorrect type casting later. For instance, if you attempt to cast the result of 12.322,11T (cleaned to 12.322,11) into a float using (float), PHP will return 0 because it cannot parse the comma as a decimal point.

This trade-off is crucial: you must decide whether your downstream logic expects a formatted string or a numeric value. If you need a standard numeric type later, you might need to normalize the delimiters after cleaning the non-numeric characters, perhaps swapping commas for periods if the locale dictates it.

Performance Considerations

For high-volume data processing, regex is generally performant enough for simple character filtering. However, if you are dealing with millions of rows, you should consider whether a full regex match is necessary or if a manual loop over the string bytes would be faster. In practice, the overhead of preg_replace is negligible for typical string lengths found in financial records.

If you are working within the Laravel ecosystem, this logic can easily be encapsulated in a custom helper method or a Model scope. If you require robust number parsing across various locales, consider leveraging Laravel's built-in localization features or libraries like illuminate/database which handle casting with specific rulesets. For more complex validation needs beyond simple string cleaning, resources like the official documentation at https://laravelcompany.com can provide deeper insights into handling data types and sanitization strategies within your application architecture.

Final Implementation Note

Always test your cleaning logic against edge cases where the input string is empty or contains only non-numeric characters. Since the regex removes everything except the allowed list, a string like ABC will become an empty string "". Ensure your downstream code handles empty strings gracefully to prevent errors when performing mathematical operations or database queries.

Our network