I am looking to get the filename from the end of a filepath string, say
$text = "bob/hello/myfile.zip";
I want to be able to obtain the file name, which I guess would involve getting everything after the last slash as a substring. Can anyone help me with how to do this is PHP? A simple function like:
$fileName = getFileName($text);
The function splitext() in os. path allows you to separate the root and extension of a specified file path. A tuple made up of the root string and the extension string is the function's output.
Sometimes the users need to read the basename of the file only by removing the file extension. Filename and extension can be separated and stored on different variables in Linux by multiple ways. Bash built-in command and shell parameter expansion can be used to remove the extension of the file.
Check out basename()
.
For more general needs, use negative value for start parameter.
For e.g.
<?php
$str = '001234567890';
echo substr($str,-10,4);
?>
will output
1234
Using a negative parameter means that it starts from the start'th character from the end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With