Michdd Posted March 29, 2009 Share Posted March 29, 2009 If I have a string like 546.gif and I want to remove the .gif part just to display the number, how can I do this? Quote Link to comment https://forums.phpfreaks.com/topic/151624-solved-string-editing/ Share on other sites More sharing options...
wildteen88 Posted March 29, 2009 Share Posted March 29, 2009 Use substr or use pathinfo $file = '546.gif'; echo substr($file, -4); // returns 546 // OR use the following PHP 5.2.x ONLY $info = pathinfo($file); echo $info['filename']; Quote Link to comment https://forums.phpfreaks.com/topic/151624-solved-string-editing/#findComment-796311 Share on other sites More sharing options...
Michdd Posted March 29, 2009 Author Share Posted March 29, 2009 Use substr or use pathinfo $file = '546.gif'; echo substr($file, -4); // returns 546 // OR use the following PHP 5.2.x ONLY $info = pathinfo($file); echo $info['filename']; Thanks, I just thought of another way.. I couldn't I just use explode()l and explode by the "."? Then use the first element of the array? Edit: Just to let you know yours does the opposite. It returns ".gif". I used: $id = explode(".", $file); echo $id[0]; Quote Link to comment https://forums.phpfreaks.com/topic/151624-solved-string-editing/#findComment-796312 Share on other sites More sharing options...
wildteen88 Posted March 29, 2009 Share Posted March 29, 2009 Yes, that is another way of doing what you want Edit: Just to let you know yours does the opposite. It returns ".gif". I used: Oh, poo! It should of been echo substr($file, 0, -4); // returns 546 Quote Link to comment https://forums.phpfreaks.com/topic/151624-solved-string-editing/#findComment-796315 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.