I'm looking for a way to get a substring in php. Given string is: "abc/def/ghi/name.extension"
I only want to have 'name', and the number of forward slashes is not known and not the same number either. Tried it with substring, but I get stuck in the elimination of the /
Anyone an idea how to solve this?
Help is greatly appreciated!
-
Duplicate of stackoverflow.com/q/2183486/134824evilReiko– evilReiko2018年03月28日 05:45:13 +00:00Commented Mar 28, 2018 at 5:45
-
1STOP answering 5 is enough only one of them is any gooduser9487972– user94879722018年03月28日 05:49:09 +00:00Commented Mar 28, 2018 at 5:49
-
This should be closed as a duplicate anyway rather than answering something that has already been answered.Nigel Ren– Nigel Ren2018年03月28日 06:13:21 +00:00Commented Mar 28, 2018 at 6:13
3 Answers 3
Try this. First split the string into an array with explode () and then split the last element by the dot.
$a = 'abc/def/ghi/name.extension';
$b = explode ('/', $a);
$b = $b[count ($b) - 1];
$c = explode ('.', $b)[0];
answered Mar 28, 2018 at 5:45
wayneOS
1,4251 gold badge14 silver badges20 bronze badges
Comments
You can try this for filename.
$filename = pathinfo("abc/def/ghi/name.extension", PATHINFO_FILENAME);
echo $filename;
and this for file extension.
$ext = pathinfo("abc/def/ghi/name.extension", PATHINFO_EXTENSION);
echo $ext;
Comments
lang-php