Stripping Numbers & File Extension from String

Hi,

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?

Thanks.
JiveMediaAsked:
Who is Participating?
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!
0
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.
0
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?
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

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.
0
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

0

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!!!
0
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

0
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.
0
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
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.