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.
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
Use:
.+\.(js|css|etc)[?]?
extension in $matches[1]
Or you can just use
.+\.(js|css|etc)\?
if the final ?cache... is always used
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With