我想简单地跳过文件名以“@ 2x”结尾的文件并在此代码中实现:
$fullres = glob("gallery/*.*");
for ($i=0; $i<count($fullres); $i++)
{
$num = $fullres[$i];
echo '<a href="'.$num.'" ><img src="/slir/?w=60&h=80&c=3x4&q=85&i=/'.$num.'" alt="" /></a>';
}
实际上可能吗?
答案 0 :(得分:0)
是的,你可以使用substr();
if(substr($num, -3) == '@2x') continue;
在定义$ num后添加此行。
您也可以简化代码
<?php
$fullres = glob("gallery/*.*");
foreach($fullres as $num)
{
if(substr($num, -3) == '@2x') continue;
echo '<a href="'.$num.'" ><img src="/slir/?w=60&h=80&c=3x4&q=85&i=/'.$num.'" alt="" /></a>';
}
?>
使用 DirectoryIterator
的解决方案<?php
foreach (new DirectoryIterator('gallery/') as $fileInfo) {
if($fileInfo->isDot() || substr($fileInfo->getFileName(), -3) == '@2x')) continue;
echo '<a href="'.$fileInfo->getFilename().'" ><img src="/slir/?w=60&h=80&c=3x4&q=85&i=/'.$fileInfo->getFilename().'" alt="" /></a>';
}
?>
答案 1 :(得分:0)
你不应该使用substr()
cuz而不是假设文件名没有任何句号,而是使用pathinfo
<?php
$fullres = glob("gallery/*.*");
foreach($fullres as $num) {
$fetch_file_name = pathinfo($num); //Fetch the file name with extension
$match_str = substr($fetch_file_name['filename'], -3); //Crop the file name
if($match_str != '@2x') {
echo '<a href="'.$num.'" ><img src="/slir/?w=60&h=80&c=3x4&q=85&i=/'.$num.'" alt="" /></a>';
}
}
?>
答案 2 :(得分:0)
选项是使用preg_grep()
过滤从glob()
返回的数组。
$fullres = glob("gallery/*.*");
$files = preg_grep('/@2x$/', $fullres, PREG_GREP_INVERT);
foreach ($files as $num)
{
// ...
}
答案 3 :(得分:0)
enter code here
$fullres = glob("gallery/*.*");
for ($i = 0; $i < count($fullres); $i++) {
$num = $fullres[$i];
$info = pathinfo($num);
$file_name = basename($num,'.'.$info['extension']);
if(substr($file_name, -3) != "@2x"){
echo '<a href="'.$num.'" ><img src="/slir/?w=60&h=80&c=3x4&q=85&i/'.$num.'" alt="" /></a>';
}
}
试试这个