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!

asked Mar 28, 2018 at 5:40
3
  • Duplicate of stackoverflow.com/q/2183486/134824 Commented Mar 28, 2018 at 5:45
  • 1
    STOP answering 5 is enough only one of them is any good Commented Mar 28, 2018 at 5:49
  • This should be closed as a duplicate anyway rather than answering something that has already been answered. Commented Mar 28, 2018 at 6:13

3 Answers 3

5

You can use pathinfo:

$str = "abc/def/ghi/name.extension";
$name = \pathinfo( $str, \PATHINFO_FILENAME );
answered Mar 28, 2018 at 5:43
Sign up to request clarification or add additional context in comments.

Comments

-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];
answered Mar 28, 2018 at 5:45

Comments

-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;
answered Mar 28, 2018 at 6:51

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.