1

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!

user2037412
  • 164
  • 1
  • 11

3 Answers3

5

You can use pathinfo:

$str = "abc/def/ghi/name.extension";
$name = \pathinfo( $str, \PATHINFO_FILENAME );
Paul
  • 139,544
  • 27
  • 275
  • 264
-1

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];
wayneOS
  • 1,427
  • 1
  • 14
  • 20
-1

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;
Praveen
  • 333
  • 1
  • 7