如何捕获和提交文件名
输入:
news.jpg
news_1.png
asnews_2.gif
asnews_3.jpeg
aw_news_4.jpg
aw_news.gif
Outout:
Array
(
[0] => Array
(
[0] => news
[1] => news_1
[2] => asnews_2
[3] => asnews_3
[4] => aw_news_4
[5] => aw_news
)
[1] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] =>
)
[2] => Array
(
[0] => news
[1] => news
[2] => asnews
[3] => asnews
[4] => aw_news
[5] => aw_news
)
)
我使用 preg_match_all 和 preg_replace_callback 在PHP中尝试过,我不知道如何正确使用。
preg_match_all('/(?: _???_ (?:_([\d]+))?$/im', $fullname, $result); // $fullname - string
这是一个类似的例子 - /(?:(?:_([\ d] +))?(。[^。] +))$ / 。你可以改变它。
答案 0 :(得分:2)
$result = array (array (), array (), array ());
$dir = opendir ("/tmp");
while (($file = readdir ($dir)) !== false)
{
if ($file == '.' || $file == '..') continue;
$matches = array ();
preg_match ('/^(([^.]*?)(?:_([0-9]*))?)(?:\.|$)/', $file, $matches);
$result [0][] = $matches [1];
$result [1][] = $matches [3];
$result [2][] = $matches [2];
}
closedir ($dir);
print_r ($result);
输出是:
Array
(
[0] => Array
(
[0] => news
[1] => news_1
[2] => asnews_2
[3] => asnews_3
[4] => aw_news_4
[5] => aw_news
)
[1] => Array
(
[0] =>
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] =>
)
[2] => Array
(
[0] => news
[1] => news
[2] => asnews
[3] => asnews
[4] => aw_news
[5] => aw_news
)
)
答案 1 :(得分:0)
在php.Explode中尝试explode
函数,该字符串包含filename.Use'。'作为爆炸分隔符和extraxt分解数组来获取文件名。
有关爆炸手册的参考,请尝试此
答案 2 :(得分:0)
使用basename()功能检索没有扩展名的文件名。
basename()
- 返回路径的文件名组件