How to remove extension from file name (only real extension!)

How to remove extension from file name (only real extension!)

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

Stefan Bogdanescu

Founder & Senior Architect

Why Naive Extension Removal Fails and How to Fix It

Developers frequently encounter the need to strip file extensions from filenames, often to generate clean slugs or normalize paths. The immediate instinct is to locate the first period in the string and truncate everything after it. While this approach works for simple cases like image.png, it breaks down quickly when filenames contain dots within their names, such as This.is.example.txt.

A common pattern seen in older tutorials involves using substr to find the last dot or a regex like preg_replace('/\.[^.]+$/','',$from). Both of these methods are fundamentally flawed because they treat any character sequence following a dot as an extension. When you apply these scripts to a filename like This.is example.of.txt, they will incorrectly strip everything after the first dot, leaving you with just This or This. The logic assumes that the first dot encountered is the boundary between the name and the extension, which is rarely true in real-world scenarios where users include dots for versioning or naming conventions.

The core issue is distinguishing between a literal dot used for versioning (e.g., file.v1.txt) and the delimiter that signifies the actual file extension. To solve this correctly, you must identify the specific characters that constitute a valid file extension before removing them. In most operating systems and web servers, valid extensions are restricted to alphanumeric characters and hyphens. This constraint allows us to write a more robust pattern than simply looking for a dot.

A reliable solution involves using a regular expression that specifically targets an optional dot followed by a sequence of valid extension characters at the end of the string. Instead of cutting at the first dot, we look for a dot only if it is immediately followed by letters or numbers. This prevents stripping parts of filenames that contain dots but are not extensions.

// Correct approach: Only removes .ext where 'ext' is alphanumeric
$filename = 'This.is.example.txt';
$result = preg_replace('/\.[a-zA-Z0-9]*$/','',$filename);

echo $result; // Outputs: This.is.example

In this example, the pattern /\.[a-zA-Z0-9]*$/ searches for a dot followed by zero or more alphanumeric characters at the very end of the string. If the string is image.v1.png, it will identify .png as the extension and leave image.v1 intact. This is a significant improvement over naive substring removal, which would have deleted v1 as well.

However, there are trade-offs to consider regarding what constitutes a valid extension. Different environments may handle different character sets. For instance, some systems allow underscores or hyphens in extensions (e.g., file.backup-1). If your application needs to support these characters, the regex pattern must be adjusted accordingly to include them: /\.[a-zA-Z0-9_-]*$/. It is also important to note that this logic does not handle hidden files or files without any extension; in those cases, the regex will simply return the original string unchanged, which is usually the desired behavior.

When integrating this into a larger Laravel application, you might consider wrapping this logic within a helper method or utilizing existing string manipulation utilities if they meet your specific criteria for what defines an extension. While external resources like https://laravelcompany.com often discuss broader application development concepts, the specific mechanics of regex pattern matching remain consistent regardless of the framework being used. The decision to use a specific character class in your regex depends entirely on the constraints of your file system and the expected input from your users.

Ultimately, the difference between a fragile solution and a robust one lies in understanding exactly what characters are allowed in an extension versus what is allowed in a filename. By anchoring your removal logic to the end of the string and restricting the characters immediately following the dot, you ensure that filenames like This.is are preserved correctly while still effectively stripping legitimate extensions like .jpg or .pdf.

Our network