我们如何删除$ file_info [path]末尾的Slash'/'[重复]

This question already has an answer here:

I am fetching Folder Path with the help of values stored in database, I am getting exact path name with $file_info[path] but this is getting output with / at end of every folder.

This above code is showing path like :

/folders/New Files/Latest/

And I want result like

/folders/New Files/Latest

Guys Please tell me exact solution for this query.

</div>

It would probably be best to correct your variable so it doesn't append the last /.

In the meantime though I'd use rtrim.

echo rtrim('/folders/New Files/Latest/', '/');

Output:

/folders/New Files/Latest

Demo: https://eval.in/463160

This way you can be sure you are only removing a /s from the end of the string. (Note if /// were the ending and you only want one to be removed this wouldn't do that, this will remove all trailing /s).

You can verify the last position of a / in the path:

if (strrpos($path, '/')==strlen($path)-1){
    // remove last character
    $path = substr($path,0,strlen($path)-1);
}

You can use substr() for this.

substr($file_info[path], 0, -1);

If the value is '/folders/New Files/Latest/', it will now be '/folders/New Files/Latest'.