Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP How to remove last part of a path

Tags:

php

I have a path like this:

parent/child/reply

How do I use PHP to remove the last part of the path, so that it looks like this:

parent/child

like image 252
Mark Avatar asked Mar 12 '10 02:03

Mark


4 Answers

dirname($path)

And this is the documentation.

like image 157
zneak Avatar answered Nov 05 '22 17:11

zneak


dirname(). You can use it as many times as you'd like

  • to get parent/child - dirname('parent/child/reply')
  • to get parent - dirname(dirname('parent/child/reply'))
like image 35
Ivan Peevski Avatar answered Nov 05 '22 19:11

Ivan Peevski


dirname()

like image 7
Ignacio Vazquez-Abrams Avatar answered Nov 05 '22 18:11

Ignacio Vazquez-Abrams


    preg_replace("/\/\w+$/i","",__DIR__);
     # Note you may also need to add .DIRECTORY_SEPARATOR at the end.
like image 3
Bill Avatar answered Nov 05 '22 18:11

Bill