sKunKbad Posted July 18, 2013 Share Posted July 18, 2013 (edited) I'm combing through somebody else's php script (an upload class) and I'm trying to figure out exactly what a preg_replace function is trying to find and replace: $this->upload_path = preg_replace("/(.+?)\/*$/", "\\1/", $this->upload_path); I've tried going to some of the online regex testing sites and testing paths, but not able to get a match with anything I've tried. To me it looks like it "any path should have one final slash, but not more than one, and not less than one". Am I close? Edited July 18, 2013 by sKunKbad Quote Link to comment https://forums.phpfreaks.com/topic/280269-analysis-of-regex-in-preg_replace/ Share on other sites More sharing options...
Solution requinix Posted July 18, 2013 Solution Share Posted July 18, 2013 (edited) That's the end result, yes. The first part is the .+? which matches as few characters as possible. Since the next part is a simple \/* then it would match everything up to but not including any trailing slashes. It's quite inefficient though (~3x slower for me), and certainly harder to understand than the equivalent string function-based $this->upload_path = rtrim($this->upload_path, "/") . "/"; Edited July 18, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/280269-analysis-of-regex-in-preg_replace/#findComment-1441198 Share on other sites More sharing options...
sKunKbad Posted July 18, 2013 Author Share Posted July 18, 2013 Thanks requinix. Your proposed code is way better. Quote Link to comment https://forums.phpfreaks.com/topic/280269-analysis-of-regex-in-preg_replace/#findComment-1441201 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.