roydukkey Posted August 30, 2008 Share Posted August 30, 2008 if i have: var = "/Assignment1/images/doors" how can i use preg_replace to gain: var = "/Assignment1/images" Thanks roydukkey Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/ Share on other sites More sharing options...
nrg_alpha Posted August 30, 2008 Share Posted August 30, 2008 You can use something like: $str = '/Assignment1/images/doors'; $str = preg_replace('#(/\w+)$#', '', $str); echo $str; EDIT: If you need to have the 'var =' in there, simply replace the first line in the snippet above with: $str = 'var = /Assignment1/images/doors'; However, I have a feeling var is supposed to be a string variable (which misses the $ before it).. so just replace any '$str' part in the code with $var... Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/#findComment-629670 Share on other sites More sharing options...
effigy Posted September 2, 2008 Share Posted September 2, 2008 The parentheses are unnecessary: #/\w+$#. Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/#findComment-631939 Share on other sites More sharing options...
nrg_alpha Posted September 2, 2008 Share Posted September 2, 2008 You're right. I have a bad habit of gouping (I suppose I have to be more mindful of un-necessary grouping as it creates extra work for regex). Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/#findComment-631955 Share on other sites More sharing options...
roydukkey Posted September 10, 2008 Author Share Posted September 10, 2008 ok but there is a problem, this works great until there are hyphens in the variable. ex. $str = '/Assignment1/images/doors/09-10-2008'; $str = preg_replace('#(/\w+)$#', '', $str); echo $str; $str equals '/Assignment1/images/doors/09-10-2008' when $str should equal '/Assignment1/images/doors' How could this be fixed? thx Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/#findComment-638681 Share on other sites More sharing options...
nrg_alpha Posted September 11, 2008 Share Posted September 11, 2008 Is this what you are looking for? //$str = '/Assignment1/images/doors/09-10-2008'; // ouputs /Assignment1/images/doors //$str = '/Assignment1/images/doors'; // outputs /Assignment1/images $str = preg_replace('#/[^/]+$#', '', $str); echo $str; Alternate Uncommenting one, then the other. This should be along the lines of what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/#findComment-638745 Share on other sites More sharing options...
Zane Posted September 11, 2008 Share Posted September 11, 2008 If that's all you're trying to do is get the end part of all the slashes, you should just be able to use pathinfo() $str = '/Assignment1/images/doors/09-10-2008'; $strInfo = pathinfo($str); echo $strInfo['basename'] // Quote Link to comment https://forums.phpfreaks.com/topic/121979-preg_replace/#findComment-638799 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.