The first regex I have will not work, with a filename like: php-gb_0.8.0.tar.gz

This is the code I have.
I have tried to fix it in my own way.
It seems to work, but there must be a more elegant way.
With only one regex.

    public function getExt()
    {
        preg_match("@\.(.+)$@", $this->name, $match);
        $case1 = strtolower($match[1]);
        preg_match("@\.(tar.gz)$@", $this->name, $match);
        $case2 = strtolower($match[1]);
        if ($case2)
            $this->extension = $case2;
        else
            $this->extension = $case1;
        return $this->extension;
    }

    I found a better way.
    I make use of pathinfo() ... with the exception of 'tar.gz'

        public function getExt()
        {
            if (preg_match("@\.tar.gz$@i", $this->name))
                $this->extension = 'tar.gz';
            else
                $this->extension = strtolower(pathinfo($this->name, PATHINFO_EXTENSION));
            return $this->extension;
        }
    

      You need another "\" before the second dot, just in case there's a file named "foo.tarxgz". 😉

        Write a Reply...