File types

This was another question in a skills evaluation test

Write a regular expression for the following code that will trim off the extension from a filename:

$ext = preg_replace(‘/xxxxx/’, ’’, $filename);

My solution:

$ext = preg_replace('/^.*\.([^.]+)$/D', '$1', $filename);

I find regular expressions pretty tricky, so usually end up using an online tool like this:

In any case, my solution to this problem would most likely have been more like:

$ext = pathinfo($filename, PATHINFO_EXTENSION);

By foxbeefly

PHP / MySQL Developer

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.