PHP How to remove last part of a path

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


Solution 1:

dirname($path)

And this is the documentation.

Solution 2:

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'))

Solution 3:

dirname()

Solution 4:

    preg_replace("/\/\w+$/i","",__DIR__);
     # Note you may also need to add .DIRECTORY_SEPARATOR at the end.