sKunKbad Posted July 18, 2013 Share Posted July 18, 2013 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? Link to comment https://forums.phpfreaks.com/topic/280269-analysis-of-regex-in-preg_replace/ Share on other sites More sharing options...
requinix Posted July 18, 2013 Share Posted July 18, 2013 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, "/") . "/"; 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. 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
Archived
This topic is now archived and is closed to further replies.