aximbigfan Posted September 15, 2008 Share Posted September 15, 2008 Hi, I need to find a way to remove everything after the last "/" in a string. For example, this: /this/is/a/string Needs to become this /this/is/a Thanks! Chris Link to comment https://forums.phpfreaks.com/topic/124270-solved-remove-everything-after-the-last-quotquot/ Share on other sites More sharing options...
genericnumber1 Posted September 15, 2008 Share Posted September 15, 2008 if you're doing it for directories, just use dirname($str); if you don't want it to apply to \ on windows then use something like substr($str, 0, strrpos($str, '/')); Link to comment https://forums.phpfreaks.com/topic/124270-solved-remove-everything-after-the-last-quotquot/#findComment-641721 Share on other sites More sharing options...
aximbigfan Posted September 15, 2008 Author Share Posted September 15, 2008 Perfect, thanks! And yeah, it is for dirs. It is for renaming the top level dir. Chris Link to comment https://forums.phpfreaks.com/topic/124270-solved-remove-everything-after-the-last-quotquot/#findComment-641722 Share on other sites More sharing options...
aximbigfan Posted September 15, 2008 Author Share Posted September 15, 2008 Darn, it's not working. Here's my code. The idea is that I have a path like this, aaaa/bbb/ccc I want to rename ccc to something else. So I wanted to use the code I requested ot get the path before ccc, so I could append the new name for ccc on to it, and rename it there. SNIP.... { $old_new = substr($dir, 0, strpos($dir, '/')); $path_old = $this->opt_regd . $dir; $path_new = $this->opt_regd . $old_new . $new; } if (!rename($path_old, $path_new)) /SNIP... EDIT: I'm an idiot. I missed that extra "r". I'll blame it on my laptop keyboard. Clearly I typed it, but the keyboard dropped it. Thanks! Thanks, Chris Link to comment https://forums.phpfreaks.com/topic/124270-solved-remove-everything-after-the-last-quotquot/#findComment-641724 Share on other sites More sharing options...
genericnumber1 Posted September 15, 2008 Share Posted September 15, 2008 glad you got it working you might consider using dirname though, as it's more portable. Link to comment https://forums.phpfreaks.com/topic/124270-solved-remove-everything-after-the-last-quotquot/#findComment-641728 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.