我有一个图像目录,它有超过800个图像,我想用一些逻辑替换它们的名字......
逻辑是;查找文件并将其替换为特定目录中名称中的“酷”到“热”字。
我的目录:/ images
我想用“热”字取代这些词:“酷”。
Old:images / this-is-cool-weather.jpg
新:images / this-is-hot-weather.jpg
我怎么能用PHP做到这一点?
答案 0 :(得分:3)
如果你做了一些研究,你需要5分钟才能做到这一点
<?php
foreach (glob("images/*.jpg") as $filename)
{
$newname=str_replace("cool","hot",$filename);
rename($filename, $newname); //add some error checking here, dont assume.
}
?>
如果您有适当的权限:)
答案 1 :(得分:2)
这应该适合你:
在这里,我只抓取包含单词&#34; cool&#34;与glob()
。在此之后,我会浏览所有文件并rename()
文件和str_replace()
&#34; cool&#34;到&#34;热&#34;
<?php
$files = glob("images/*cool*.jpg");
foreach($files as $file)
rename($file, dirname($file) . "/" . str_replace("cool", "hot", basename($file)));
?>
答案 2 :(得分:1)
您可能希望实现类似
的内容<?php
foreach (glob("*.jpg") as $filename) {
rename($filename, str_replace('cool','hot', $filename));
}
?>
请注意,这没有异常处理和/或其他指针。 这应该让你开始