thiebo
thiebo

Reputation: 1435

php preg_replace string that ends and finishes with slash character

dirname($_SERVER['PHP_SELF'])

returns in my case : /blabla/test

I am trying a preg_replace so to remove /blabla/.

I do this :

$dossier = preg_replace('/^\/[0-9a_z]\/$/','',dirname($_SERVER['PHP_SELF']));

what I hoped to be doing was : "find anything that starts with / and ends with / and has 0-9a-z in between. But this is not how to do this.

Thanks

Upvotes: 0

Views: 522

Answers (2)

Iwnnay
Iwnnay

Reputation: 2008

Putting that dollar sign in there is throwing off the regex. I believe this is what you're looking for. Let me know if that doesn't work.

$dossier = preg_replace('/^\/[0-9a-z]*\//', '', dirname($_SERVER('PHP_SELF']));

Upvotes: 1

trincot
trincot

Reputation: 350079

Three issues:

  • You need a hyphen between a-z, not an underscore
  • You need to allow for more than one such character, by adding a +
  • You don't want to match the whole string (to be removed), so do not require to match until the end: remove the $

So this will do it:

/^\/[0-9a-z]+\//'

Upvotes: 0

Related Questions