Jump to content

Analysis of regex in preg_replace


sKunKbad

Recommended Posts

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

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, "/") . "/";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.