Jump to content

preg_replace help needed


keevitaja

Recommended Posts

~/[\w]+/?$~

 

~ - opening delimiter

/ - a literal forward slash

[\w]+ - 1 or more 'word' characters (letters, digits, and underscores).

/? - an optional literal forward slash

$ - end of string

~ - closing delimeter

 

It probably makes more sense to the human brain in this case if you read it (and the path) backwards. It will include the trailing slash if there is one, then will work through untill it finds the next(previous) forward slash. These matches characters you would then replace with nothing.

 

$path = preg_replace("~/[\w]+/?$~", "", $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.