Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to extract file extension from URL

Tags:

regex

php

I am looking for a regex that would match .js in the following URI:

 /foo/bar/file.js?cache_key=123

I'm writing a function that tries to identify what kind of file is being passed in as a parameter. In this case, the file ends with the extension .js and is a javascript file. I'm working with PHP and preg_match so I'm assuming this is a PCRE compatible regular expression. Ultimately I'll build on this expression and be able to check for multiple file types that are being passed in as a URI that isn't just limited to js, but perhaps css, images, etc.

like image 631
randombits Avatar asked Mar 18 '23 00:03

randombits


2 Answers

You can use a combination of pathinfo and a regular expression. pathinfo will give you the extension plus the ?cache_key=123, and you can then remove the ?cache_key=123 with a regex that matches the ? and everything after it:

$url = '/foo/bar/file.js?cache_key=123';

echo preg_replace("#\?.*#", "", pathinfo($url, PATHINFO_EXTENSION)) . "\n";

Output:

js

Input:

$url = 'my_style.css?cache_key=123';

Output:

css

Obviously, if you need the ., it's trivial to add it to the file extension string.

ETA: if you do want a regex solution, this will do the trick:

function parseurl($url) {
    # takes the last dot it can find and grabs the text after it
    echo preg_replace("#(.+)?\.(\w+)(\?.+)?#", "$2", $url) . "\n";
}

parseurl('my_style.css');
parseurl('my_style.css?cache=123');
parseurl('/foo/bar/file.js?cache_key=123');
parseurl('/my.dir.name/has/dots/boo.html?cache=123');

Output:

css
css
js
html
like image 78
i alarmed alien Avatar answered Mar 21 '23 05:03

i alarmed alien


Use:

.+\.(js|css|etc)[?]?

extension in $matches[1]

Or you can just use

.+\.(js|css|etc)\?

if the final ?cache... is always used

like image 32
Croises Avatar answered Mar 21 '23 03:03

Croises