Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove text between last 2 slashes (PHP)

Tags:

php

I have made a system with browsing files; Now I have a problem:

My path string like this: A/Abba/2004/

I want to remove the text between the last 2 slashes (which is 2004 in this case).
So the output should look like A/Abba/

Can anyone help me as how to archive this in php?

Thanks.

like image 658
James Bob Avatar asked Jul 16 '26 14:07

James Bob


1 Answers

If you try to remove last part of string you can do this this way:

$input = 'A/Abba/2014/';
$array = array_filter(explode('/', $input));
array_pop($array);
$correct = implode('/', $array);

array_filter removes empty strings from array

explode converts string to array by given delimiter

array_pop remove last item from given array

implode converts array to string using given 'glue'

like image 133
piotr Avatar answered Jul 20 '26 21:07

piotr



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!