Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

php - How to remove extension from string (only real extension!)

I'm looking for a small function that allows me to remove the extension from a filename.

I've found many examples by googling, but they are bad, because they just remove part of the string with "." . They use dot for limiter and just cut string.

Look at these scripts,

$from = preg_replace('/.[^.]+$/','',$from);

or

 $from=substr($from, 0, (strlen ($from)) - (strlen (strrchr($filename,'.'))));

When we add the string like this:

This.is example of somestring

It will return only "This"...

The extension can have 3 or 4 characters, so we have to check if dot is on 4 or 5 position, and then remove it.

How can it be done?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

http://php.net/manual/en/function.pathinfo.php

$filename = pathinfo('filename.md.txt', PATHINFO_FILENAME); // returns 'filename.md'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...