Jump to content

[SOLVED] problem with preg_match


Vidya_tr

Recommended Posts

You need to make the first parenthesized pattern non-greedy, so it won't just match the whole string, and leave nothing for the rest of the pattern. Do it by adding a question mark after the asterisk:

 

<?php
$name = 'sample.flv';
preg_match('/^(.*?)(\..*)?$/', $name, $matches);
$extension = $matches[2];
?>

 

But that code will only work on filenames without dots in the name. A simple and faster way to get the file extension every time:

 

<?php
$name = 'sample.new.flv';
$parts = explode('.', $name);
$extension = end($parts);
?>

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.