dreamwest Posted January 10, 2009 Share Posted January 10, 2009 How can i strip an extension from a string?? I like crosscountry.wmv To be just: I like crosscountry Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/ Share on other sites More sharing options...
abdfahim Posted January 10, 2009 Share Posted January 10, 2009 $myvar="I like crosscountry.wmv"; $myarr=explode(".",$myvar); echo $myarr[0]; if you have any other dot (.) in the string, its better to use preg_match Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734016 Share on other sites More sharing options...
nuttycoder Posted January 10, 2009 Share Posted January 10, 2009 or use str_replace: <?php $theFile = 'I like crosscountry.wmv'; $theFile = str_replace("I like crosscountry.wmv","I like crosscountry",$theFile); echo $theFile; //outputs I like crosscountry ?> Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734017 Share on other sites More sharing options...
abdfahim Posted January 10, 2009 Share Posted January 10, 2009 @nuttycoder .. how can you know what is there before dot(.) ?? Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734018 Share on other sites More sharing options...
nuttycoder Posted January 10, 2009 Share Posted January 10, 2009 Sorry not sure what you mean. Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734020 Share on other sites More sharing options...
abdfahim Posted January 10, 2009 Share Posted January 10, 2009 see, if you know what to replace with, then you dont have to do anything. for instant, I wrote "I love abc.com", person B wrote "I love def.com" etc.... now, when you write your code, you have to replace my string with "I love abc", person B's string with "I love def" and so on. If you use str_replace only, then how can you do it? Its not a constant string definitely. It must vary with user, otherwise, if as a coder you know the string which is constant, why should you need to strip that with code? Just use the Backspace buton on keyboard and delete the unnecessary portion !!!! Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734025 Share on other sites More sharing options...
nuttycoder Posted January 10, 2009 Share Posted January 10, 2009 yeah I see your point :0) Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734028 Share on other sites More sharing options...
void Posted January 10, 2009 Share Posted January 10, 2009 oh, come on guys. $str = 'any.title.with.many.dots.wmv'; $title = substr($str,0,strrpos($str,".")); Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734029 Share on other sites More sharing options...
abdfahim Posted January 10, 2009 Share Posted January 10, 2009 yes void .. there is always many ways to do a thing in php ... no doubt on that .... its not a rocket science .... no need to be impatient ... Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734030 Share on other sites More sharing options...
kenrbnsn Posted January 10, 2009 Share Posted January 10, 2009 I would use the pathinfo function: <?php $myvar="I like crosscountry.wmv"; $x = pathinfo($myvar); echo $x['filename']; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734032 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 $e = explode(".",$fileName); unset($e[count($e) - 1]); $fileName = implode(".",$e); Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734034 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 I would use the pathinfo function: <?php $myvar="I like crosscountry.wmv"; $x = pathinfo($myvar); echo $x['filename']; ?> Ken or yeah.. pathinfo($fileName,PATHINFO_FILENAME); but idk if pathinfo() will do it if that file doesn't exist :S Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734038 Share on other sites More sharing options...
Daniel0 Posted January 10, 2009 Share Posted January 10, 2009 but idk if pathinfo() will do it if that file doesn't exist :S It will. Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734039 Share on other sites More sharing options...
kenrbnsn Posted January 10, 2009 Share Posted January 10, 2009 pathinfo works fine whether or not the file exists: <?php $fe = (file_exists('no.such.file.here'))?'exists':'does not exist'; echo 'file no.such.file.here ' . $fe . "\n"; echo pathinfo('no.such.file.here',PATHINFO_FILENAME) . "\n"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/140283-strip-extensions/#findComment-734046 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.