0

My input value is just like this: Oppa/upload/default.jpeg

I want to slice the value of an input according by / cause i want to get the image file name. Does anyone know some tricks to do this?

example: i want to get default.png

<input type="text" value="Oppa/upload/default.png" id="fileLink" name="fileLink" />
random_user_name
  • 25,694
  • 7
  • 76
  • 115
user3842025
  • 75
  • 1
  • 1
  • 8

6 Answers6

3

Use basename():

$path = "Oppa/upload/default.jpeg";
echo basename($path); //will output "default.jpeg"
echo basename($path, '.jpeg'); //will output "default"

The first parameter is the path of which the trailing component will be removed. If the first parameter ends in the optional second parameter, the second parameter will also be cut off.

On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/). - PHP manual

Jonan
  • 2,485
  • 3
  • 24
  • 42
1

You should use basename() PHP function.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
1

This will work for you

$path = "Oppa/upload/default.jpeg";
echo basename($path);
Mritunjay
  • 25,338
  • 7
  • 55
  • 68
1

Use pathinfo() php function

$path = "http://domain.tld/Oppa/upload/default.png";
$info = pathinfo ( $path, PATHINFO_BASENAME ); // returns default.png
David Jacquel
  • 51,670
  • 6
  • 121
  • 147
0

Yet another solution, using preg_match()

<?php
$path = "http://domain.tld/Oppa/upload/default.png";   // or "C:\\domain.tld\Oppa\upload\default.jpg";
$pattern = '/[\/|\\\\]((?:.(?!\/|\\\\))+)$/';
if(preg_match($pattern, $path, $matches)){
    echo $matches[1];  // default.png or default.jpg
}
?>

Note: People claim to have problems using basename() with asian characters

hex494D49
  • 9,109
  • 3
  • 38
  • 47
-1

I am supposing that if your image path will not be change from Oppa/upload/ than this Should work using explode :

$str = "Oppa/upload/default.jpeg";
$s= explode("Oppa/upload/",$str);
echo $s[1];

Another Best thing you can do with defining the relative path as a constant, so :

const path = "Oppa/upload/";
$str = "Oppa/upload/default.jpeg";
$s= explode(path,$str);
echo $s[1];

will also work.

Harshal
  • 3,562
  • 9
  • 36
  • 65