繁体   English   中英

如何从远程URL获取具有多个部分的文件名

[英]how to get file name with multipart from remote url

我的远程计算机上有一些这种格式的文件,如下所示:

123-0.jpeg,123-1.jpeg,123-2.jpeg,123-3.jpeg, ...
544-0.jpeg,544-1.jpeg,544-2.jpeg,544-3.jpeg, ...

现在我只想从url中获取所有以123开头的文件名(此格式为123- * .jpeg),但是我不知道如何使用以下格式来重命名该名称:123-*。jpeg也许可以做一些array或你知道吗,有人可以告诉我谢谢你吗? 或者,如果可能的话,只要获得耐久者的名字123-3.jpeg。

您在寻找glob函数吗?

http://php.net/manual/en/function.glob.php

foreach (glob("123-*.jpeg") as $filename) {
  echo "$filename size " . filesize($filename) . "\n";
}

编辑:根据您的评论,这可能会满足您的要求:它有一些缺陷(例如:远程服务器更改输出),但足以使您朝正确的方向前进。

$html = file_get_contents("http://plusmaster.info/screenshots/");

// Note that this pattern returns nothing at the present.
$pattern = '/data-name="(123-[0-9]*\.jpeg)" /';

// If you want to get *-123.jpeg instead, use this:
// $pattern = '/data-name="([0-9]*-123\.jpeg)" /';

preg_match_all($pattern, substr($html,3), $matches, PREG_PATTERN_ORDER);

// To dump the whole array of matches:
var_dump($matches[1]);

// To echo the last item in the array
echo end($matches[1]);

在PHP中使用Regex的几个有用工具:

1)尽管Rubular是针对Ruby制作的,但它通常也非常适合测试PHP regexes: http ://rubular.com/

2)PHP手册更深入地介绍了preg_match_all: http//php.net/manual/en/function.preg-match-all.php

如果您有任何疑问,请告诉我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM