I have a generator that create PDF files like this:
我有一个生成PDF文件的生成器:
and upload files into ./files/
folder on server.
并将文件上传到服务器上的./files/文件夹中。
I used below code to get array:
我使用下面的代码来获取数组:
<?php
$files = glob('files/*.{PDF,pdf}', GLOB_BRACE);
print_r($files);
OutPut:
输出:
Array
(
[0] => files/035146-761326.PDF
[1] => files/035150-710753.PDF
[2] => files/035151-771208.PDF
[3] => files/035153-718443.PDF
[4] => files/035158-219299.PDF
[5] => files/035159-667486.PDF
[6] => files/035172-113022.PDF
[7] => files/035180-482460.PDF
[8] => files/035216-232840.PDF
)
now I wanna splite each file name to user
and password
. for example if i have file like this: 035180-482460.PDF
I should have:
现在我想把每个文件名分成用户名和密码。例如,如果我有这样的文件:035180-482460.PDF我应该有:
file['user] = 035180;
file['password'] = 482460;
I know, I show foreach (files as key => value)
and some stuff to split filename; but I don't know how can I do that? :(
我知道,我显示foreach(文件为key => value)和一些东西来分割文件名;但我不知道怎么办呢? :(
3 个解决方案
#1
2
你可以使用list和explode:
<?php
foreach ($files as $file) {
$base = basename($file);
list ($user, $pass) = explode('-', substr($base, 0, strpos($base, '.')));
// $user would contain 035146
// $pass would contain 761326
}
First you get the basename (convert files/035146-761326.PDF
to 035146-761326.PDF
) then you'd use substr and strpos to only return the file name excluding the extension, then explode using -
and you get the two parts.
首先你得到基本名称(将文件/ 035146-761326.PDF转换为035146-761326.PDF)然后你使用substr和strpos只返回不包括扩展名的文件名,然后使用 - 爆炸 - 你得到两部分。
#2
4
array_map is very helpful in such cases:
在这种情况下,array_map非常有用:
$files = array_map(function($name){
preg_match('#(\d+)-(\d+)#', $name, $matches); //get user and password
return array(
'name' => $name,
'user' => $matches[1],
'password' => $matches[2]
);
}, $files);
print_r($files);
#3
2
Try:
尝试:
<?php
$files = glob('files/*.{PDF,pdf}', GLOB_BRACE);
print_r($files);
foreach($files as $file) {
$file = preg_replace('/\/(?=.*\/)/', ' ', $file); // it will solve the ./files/ issue which you mentioned in comment
// suppose $file is files/035146-761326.PDF
$arr = explode("/",$file); // it will give array( [0]=>files and [1]=> 035146-761326.PDF)
$filename = explode(".",$arr[1]); // now split $arr[1] with dot, so will give new array array([0] => "035146-761326", [1] => "pdf")
$arrname = explode("-",$filename[0]); // now split $filename[0] with - so it will give array ([0]=>035146 , [1] =>761326 )
echo "username: ".$arrname[0];
echo "password: ".$arrname[1];
}
?>
#1
2
你可以使用list和explode:
<?php
foreach ($files as $file) {
$base = basename($file);
list ($user, $pass) = explode('-', substr($base, 0, strpos($base, '.')));
// $user would contain 035146
// $pass would contain 761326
}
First you get the basename (convert files/035146-761326.PDF
to 035146-761326.PDF
) then you'd use substr and strpos to only return the file name excluding the extension, then explode using -
and you get the two parts.
首先你得到基本名称(将文件/ 035146-761326.PDF转换为035146-761326.PDF)然后你使用substr和strpos只返回不包括扩展名的文件名,然后使用 - 爆炸 - 你得到两部分。
#2
4
array_map is very helpful in such cases:
在这种情况下,array_map非常有用:
$files = array_map(function($name){
preg_match('#(\d+)-(\d+)#', $name, $matches); //get user and password
return array(
'name' => $name,
'user' => $matches[1],
'password' => $matches[2]
);
}, $files);
print_r($files);
#3
2
Try:
尝试:
<?php
$files = glob('files/*.{PDF,pdf}', GLOB_BRACE);
print_r($files);
foreach($files as $file) {
$file = preg_replace('/\/(?=.*\/)/', ' ', $file); // it will solve the ./files/ issue which you mentioned in comment
// suppose $file is files/035146-761326.PDF
$arr = explode("/",$file); // it will give array( [0]=>files and [1]=> 035146-761326.PDF)
$filename = explode(".",$arr[1]); // now split $arr[1] with dot, so will give new array array([0] => "035146-761326", [1] => "pdf")
$arrname = explode("-",$filename[0]); // now split $filename[0] with - so it will give array ([0]=>035146 , [1] =>761326 )
echo "username: ".$arrname[0];
echo "password: ".$arrname[1];
}
?>