开发者

basic question in foreach in php

开发者 https://www.devze.com 2022-12-12 13:22 出处:网络
basically if I have a string like this: $str = \\data1\\data开发者_开发知识库2\\data3\\data_tmp\\file.pgp

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

精彩评论

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