Php remove first part of path

i have a path like this:

/dude/stuff/lol/bruh.jpg

i want to echo it like this:

/stuff/lol/bruh.jpg

how to do this? and if you could please explain it. i found one good answer Removing part of path in php but i cannot work my way around with explode and implode.

please give me the link to the duplicate answer if my question is a duplicate

asked May 22, 2017 at 2:24

3

$path = '/dude/stuff/lol/bruh.jpg';
$path = explode['/', $path];
unset[$path[1]];
$path = implode['/', $path];

Will separate the string into an array, then unset the first item [technically the second in the array, since the first element will be empty due to the string starting with /]. Finally the path is imploded, and you get:

/stuff/lol/bruh.jpg

answered May 22, 2017 at 2:27

EnstageEnstage

2,07612 silver badges20 bronze badges

You could use strpos with substr:

$string = '/dude/stuff/lol/bruh.jpg';
$string = substr[$string, strpos[$string, '/', 1]];

The strpos to look for the position of the / after the first one, then use substr to get the string starting from that position till the end.

answered May 22, 2017 at 2:29

Chin LeungChin Leung

14.1k3 gold badges28 silver badges52 bronze badges

Try this :

$string = "/dude/stuff/lol/bruh.jpg";
$firstslash = strpos[$string,"/",1];
$secondstring = substr[$string, $firstslash+1];
echo "String : " . $string;
echo " 
Result : " . $secondstring;

answered May 22, 2017 at 2:39

You can use a regular expression to extract resource name you need.

preg_match['/[?P[^\/]+]$/', $path, $match];

Then you have to use $match as array, and you can see the name inside of it.

$match['name];

answered Apr 8 at 8:58

Ismael MoralIsmael Moral

7021 gold badge8 silver badges34 bronze badges

Removing the file path from a filename in PHP is be easy!

The basename[] function will remove the file’s path from your string and leave only the filename. So, for example, it means that if your file path string is: ‘/home/anto.online/apac/basepath/test.php’, then using basename[$path] will return only the filename. The result will thus be: ‘test.php’.

Functions used in conjunction

The basename[] function is often used with these PHP functions:

realpath[] – Returns the full absolute path, including the filename.

dirname[] – Can return the file path of a parent directory. If there are no slashes in the file path, a dot [‘.‘] is returned, indicating the current file path. Otherwise, the returned string is a file path with any trailing component removed.

Example of how to use these functions in PHP:

//get the absolute path of a file
$real = realpath['./test.php'];
echo 'realpath result: ' . $real . '
'; //output: /home/anto.online/apac/basepath/test.php //get the absolute path of a file $dir = dirname['./test.php']; echo 'dirname result: ' . $dir . '
'; //output: . //get the filename and remove the filepath $base = basename[$real]; echo 'basename result: ' . $base . '
'; //output: test.php

Practical example – Removing file path from error message

Let’s say you need to write an error handler in PHP. The custom handler must not expose paths to the front-end. Your remove file paths for security reasons.

We can use basepath[] to sanitize the $file variable and remove file paths.

Thus, for the custom error handler example below:

function customErrorHandler[$code, $message, $file, $line] {
$logger = createLogger[];
...

We can do this by determining the string position of the ‘/’ and then use a  substring function. [This would be the hard way!]

Alternatively, we can use the basename[] method in PHP. It even considers the differences between Windows and Linux!

Adding the basename[] function will make our error handler look like this:

function customErrorHandler[$code, $message, $file, $line] {
$logger = createLogger[];
...
$message = 'Error ID: [' . $uid . '] [' . SITE_ID . '] ' . $user . 'Message: [' . $message . '] At: [' . basename[$file] . '] Line: [' . $line . '] Code: [' . $code . ']';
...

You may also be interested in


About the Authors

Anto's editorial team loves the cloud as much as you! Each member of Anto's editorial team is a Cloud expert in their own right. Anto Online takes great pride in helping fellow Cloud enthusiasts. Let us know if you have an excellent idea for the next topic! Contact Anto Online if you want to contribute.

Support the Cause

Support Anto Online and buy us a coffee. Anything is possible with coffee and code.

Buy me a coffee


Chủ Đề