Stripping Numbers & File Extension from String


I was looking to strip all numbers/other characters and the file extension from a string using PHP probably using preg_replace?

For example, the filename stored in a variable at the moment is something like: This is File - 1.jpg

I want to chop the - 1.jpg from the string so it only shows This is File

Can someone please help?

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Maybe you can give us a few more before-and-after examples?  The concept of test-driven development is very useful when you try to define the bounds of problems like this.  Questions like "What if the name was 'This is the 3rd file.jpg' " come up.  Would the answer be "This is the rd file?"  With more test data we can probably give you a better answer!
JiveMediaAuthor Commented:
Hi Ray,

Good point, after some thought it would probably be best if i stripped everything after the - 1.jpg as the name might need to contain numbers.
Terry WoodsIT GuruCommented:
Is it only jpg's you want to target, or other extensions too? What about other numbers? Will there always be a dash and space before the number?
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

JiveMediaAuthor Commented:
Hi Terry,

Any file extension between 2 to 4 characters, so for example, it can be .ai / .jpg / .jpeg etc.

We have given our client strict file naming conventions, so it will either always be one of the following;

1) This is a file.jpg
2) This is a file - 2.jpg (the number 2 used for example, it can be - 9.jpg etc.)

Hope this clarifies it a little more.
Terry WoodsIT GuruCommented:
Try this. It requires the extension to be at the end of the input string. Let me know if that's not the case.
$mystring = preg_replace('#(- \d+)?\.[a-z]{2,4}$#i', '', $mystring);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JiveMediaAuthor Commented:
Worked awesome!!!
Terry WoodsIT GuruCommented:
If the space is before the dash (or if there's no dash, then before the extension):

$mystring = preg_replace('#\s?(- \d+)?\.[a-z]{2,4}$#i', '', $mystring);

Open in new window

Ray PaseurCommented:
Now that we have a clear definition of the problem, you might find it does not require regular expressions; easier to use explode() and trim().  Just a thought.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.