richrock Posted April 16, 2010 Share Posted April 16, 2010 Hi all, I've got a string, extracted from a database of stored filenames. I can remove the .jpg part, and I need to also remove the last part - a couple of examples: 21_amati-andrea_cremona-circa1580-rtc - need to remove '-rtc' 33_amati-antonius-and-hieronymus_cremona-circa1620-b - need to remove '-b' 35_amati-antonius-and-hieronymus_cremona-circa1620-rs - need to remove '-rs' These are view codes on the image for a page being used to show different views of an instrument. I just need to dynamically extract this part to make a SEF url. Anyone know how to remove the end part including the '-'? I'm sure I'd done it before, but cannot figure out whne or where I've checked the PHP pages, and looked through w3c schools pages but nothings registering with my brain cells... Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/ Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 for each input line... $tmp = explode('-',$input); $output = str_replace('-'.$tmp[count($tmp)-1],'',$input); Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/#findComment-1043052 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Try this: $str = substr(0, strrpos($str, "-")); Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/#findComment-1043053 Share on other sites More sharing options...
deanlearner Posted April 16, 2010 Share Posted April 16, 2010 Ooo nice, I didnt realise strrpos returned the last instance, do what Ken2k7 said. Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/#findComment-1043055 Share on other sites More sharing options...
Adam Posted April 16, 2010 Share Posted April 16, 2010 Try this: $str = substr(0, strrpos($str, "-")); That wouldn't remove past the last hyphen only store it, use substr_replace instead: $str = substr_replace($str, '', strrpos($str, '-')); Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/#findComment-1043061 Share on other sites More sharing options...
richrock Posted April 16, 2010 Author Share Posted April 16, 2010 deanlearners snippet did it for me - I'll keep that on snipplr now Thanks Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/#findComment-1043071 Share on other sites More sharing options...
Ken2k7 Posted April 16, 2010 Share Posted April 16, 2010 Oops, I left out a parameter: $str = substr($str, 0, strrpos($str, "-")); Quote Link to comment https://forums.phpfreaks.com/topic/198748-sure-ive-done-this-before-removed-end-of-string/#findComment-1043074 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.