Mcod Posted December 19, 2011 Share Posted December 19, 2011 Hi there, I need some help with modifying a string. I always have strings that look like: This is part two - This is part one This is part four - This is part three What I need is to switch the position which is separated by space minus sign space, so it returns: This is part one - This is part two This is part three - This is part four Something like: $string = "this is part two - this is part one"; after the code I am looking for ran, it would return: $newstring = "this is part one - this is part two"; Any ideas how this could be done so it works for everything that is separated by space minus space? Thank you for your time Quote Link to comment https://forums.phpfreaks.com/topic/253498-switching-text-after-minus-sign/ Share on other sites More sharing options...
Pikachu2000 Posted December 19, 2011 Share Posted December 19, 2011 explode array_reverse implode Quote Link to comment https://forums.phpfreaks.com/topic/253498-switching-text-after-minus-sign/#findComment-1299443 Share on other sites More sharing options...
ragax Posted December 20, 2011 Share Posted December 20, 2011 Hi Mcod! This regex will reverse your strings. Match expression: ([^-]*?) -(.*) Replace expression: \2 - \1 Here is an example of preg_replace integration ($subject is your string): $result = preg_replace('/([^-]*?) -(.*)/m', '\2 - \1', $subject); Is this what you were looking for? Quote Link to comment https://forums.phpfreaks.com/topic/253498-switching-text-after-minus-sign/#findComment-1299560 Share on other sites More sharing options...
Mcod Posted January 10, 2012 Author Share Posted January 10, 2012 Thank you playful, this did in fact help me a lot Quote Link to comment https://forums.phpfreaks.com/topic/253498-switching-text-after-minus-sign/#findComment-1306195 Share on other sites More sharing options...
ragax Posted January 10, 2012 Share Posted January 10, 2012 You're welcome, really glad it helped. Warmest wishes. Quote Link to comment https://forums.phpfreaks.com/topic/253498-switching-text-after-minus-sign/#findComment-1306199 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.