PHP string help
Hey, so I am trying to learn some string functions in PHP and came across a senario I cannot figure out. I开发者_如何学编程 want to take a string like such: /var/www/html
and remove everything after the last forward slash (including the forward slash) so I end up with /var/www
What would be the best way to go about this? Many thanks.
Try using dirname
. It may handle special cases for you that you may not have anticipated if you went with a string manipulation route.
This should work:
$str = substr($str, 0, strrpos($str, '/') - 1);
...but see icktoofay's solution if you're only planning on using this to handle file paths.
精彩评论