开发者

PHP string help

开发者 https://www.devze.com 2023-03-10 07:31 出处:网络
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 t

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消