another (simple) reg expression problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JustCantThinkOfAName
Forum Newbie
Posts: 2
Joined: Wed Nov 26, 2003 8:38 am

another (simple) reg expression problem

Post by JustCantThinkOfAName »

hi
I'm trying to get my head round regular expressions - i've got ones that will remove every non- a-z or 1-9 character but what i'm trying to do is make one for taking the extnsions off file names, so that

"this is my great filename.doc"

would be just

"this is my great filename"

it would need to be something that would match for a "." and then remove everything after it (in case the extension isn't a standard 3-letter one)

can anyone help please ? I'm a real noob at this ...........

thanks
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can get away without using regular expressions to do this:

Code: Select all

<?php

$file_name = 'this is my great filename.doc';
$file_info = pathinfo($file_name);
$file_no_extension = basename($file_info['basename'], '.'.$file_info['extension']);

echo 'file without extension: '.$file_no_extension;
?>
For more info:
[php_man]pathinfo[/php_man]()
[php_man]basename[/php_man]()

Mac
JustCantThinkOfAName
Forum Newbie
Posts: 2
Joined: Wed Nov 26, 2003 8:38 am

Post by JustCantThinkOfAName »

Hi - it's a big relief not to have to use a regular expression!
I'll look at those two functions in more detail
Thank you
Post Reply