开发者

basic question in foreach in php

basically if I have a string like this:

$str = \data1\data开发者_开发知识库2\data3\data_tmp\file.pgp

can anyone tell me how to get the last part 'file.pgp'?

TIA.


You are looking for the basename() function.

This function takes a file path and returns the file name without the suffix (the final part of your file name that specifies its type)


$last = array_pop(explode('\\', $str));

You don't need foreach for that. It's used when you have to iterate through the whole collection (in your case, array).

If you need to get the remaining part of the string:

$segments = explode('\\', $str);
$last = array_pop($segments);

It will be in $segments, as an array. If you want to convert it back to a string, then use join('\\', $segments). However, if this is a Windows path and you're running PHP on Windows, then you should be using the basename and dirname functions.


perhaps pathinfo() will give you what you need

if that doesn't do it try

$path = str_replace('\\', '/', $path)

first

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜