我有一个图像目录,它有800多个图像,我想用一些逻辑替换它们的名称。。。
逻辑是;在特定目录中查找文件并将其名称中的"酷"字替换为"热"字。
我的目录:/images
我想把这些词:"酷"换成"热"。
旧:images/this-is-cool-weather.jpg
新增:images/this-is-hotweather.jpg
我怎样才能用PHP做到这一点?
如果你做了一些研究,你需要5分钟才能完成
<?php
foreach (glob("images/*.jpg") as $filename)
{
$newname=str_replace("cool","hot",$filename);
rename($filename, $newname); //add some error checking here, dont assume.
}
?>
如果您有适当的权限:)
这应该适用于您:
在这里,我只抓取包含glob()
的单词"酷"的文件。之后,我浏览了所有文件和rename()
文件,str_replace()
"冷却"到"热"
<?php
$files = glob("images/*cool*.jpg");
foreach($files as $file)
rename($file, dirname($file) . "/" . str_replace("cool", "hot", basename($file)));
?>
您可能想要实现类似的东西
<?php
foreach (glob("*.jpg") as $filename) {
rename($filename, str_replace('cool','hot', $filename));
}
?>
请注意,这没有异常处理和/或其他指针。这应该会让你开始