Jump to content

Analysis of regex in preg_replace


sKunKbad
Go to solution Solved by requinix,

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?

Edited by sKunKbad
Link to comment
Share on other sites

  • Solution

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 by requinix
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.