Re[2]: [PHP-DEV] Parsing File Names
| From: | Daniel Lorch | Date: | Sun, 03 Mar 2002 22:08:57 +0000 |
| Subject: | Re[2]: [PHP-DEV] Parsing File Names | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-80657@lists.php.net to get a copy of this message | ||
hi,
> <?php
> $handle=opendir('/path/to/files');
> while (false !== ($file = readdir($handle))) {
> if (preg_match("/(.*) - (.*).mp3/i", $file, $matches)) {
> $artist = $matches[1];
> $song = $matches[2];
> // insert into db
> }
> }
?>>
I'd suggest to write it like this:
if (preg_match("/([^\-]+) - ([^\-]+)\.mp3/i", $file, $matches)) {
list(, $artist, $song) = $matches;
[..]
}
Match everything that's not a dash [^\-]+ , and escape \. as .
is a regular expression wildcard.
Daniel Lorch