vardius
vardius

Reputation: 6546

PHP how can i remove substring between last slashes?

Here is my string:

test-e2e4/folder1/folder2/6.png.

I want to remove the last part of it, so it should look like this:

test-e2e4/folder1/folder2/

How can i do that using preg_replace() ?

Upvotes: 1

Views: 107

Answers (2)

Rikesh
Rikesh

Reputation: 26431

$string = preg_replace('#[^/]*$#', '', $string);

DEMO.

Upvotes: 3

ray
ray

Reputation: 4267

besides preg_replace, you can also use dirname

echo dirname("test-e2e4/folder1/folder2/6.png") . "/";

Upvotes: 2

Related Questions