如何在php中删除名称上有特定单词的文件

如何在php中删除名称上有特定单词的文件,php,Php,在我的目录中,我有以下文件: JBBsongs.html、JBB.php、ArrangeJBB.html、Follow.html和Follow.php 我知道我可以使用取消链接 <?php unlink('test.html'); ?> 但是如何删除名称中包含“JBB”的特定文件?您可以使用readdir或scandir在文件夹中的所有文件之间循环。 一旦有了文件名,就可以检查JBB的strpo。如果strpos值等于或大于0。然后可以调用文件名上的unlink函数。这将删除

在我的目录中,我有以下文件:

JBBsongs.html、JBB.php、ArrangeJBB.html、Follow.html和Follow.php

我知道我可以使用
取消链接

<?php

unlink('test.html');
?>


但是如何删除名称中包含“JBB”的特定文件?

您可以使用readdir或scandir在文件夹中的所有文件之间循环。
一旦有了文件名,就可以检查JBB的strpo。如果strpos值等于或大于0。然后可以调用文件名上的unlink函数。这将删除文件

您可以使用readdir或scandir在文件夹中的所有文件之间循环。 一旦有了文件名,就可以检查JBB的strpo。如果strpos值等于或大于0。然后可以调用文件名上的unlink函数。这将删除文件

使用功能

假设您的文件名已经存在于数组中,您可以执行以下操作

<?php
$files = Array ('JBBsongs.html', 'JBB.php', 'ArrangeJBB.html', 'Follow.html', 'follow.php');

foreach ($files as $file)
{
    if (strstr ($file, "JBB"))
    {
        unlink('test.html');
    }
}
?>

strstr
如果第一个字符串中不存在指针(搜索的字符串),则返回
false
。这样,只有所需的文件才会被取消链接。

使用该功能

假设您的文件名已经存在于数组中,您可以执行以下操作

<?php
$files = Array ('JBBsongs.html', 'JBB.php', 'ArrangeJBB.html', 'Follow.html', 'follow.php');

foreach ($files as $file)
{
    if (strstr ($file, "JBB"))
    {
        unlink('test.html');
    }
}
?>


strstr
如果第一个字符串中不存在指针(搜索的字符串),则返回
false
。这样,只有所需的文件才会被取消链接。

您可以使用
scandir
在目录中循环,使用
strpos
检查是否包含
JBB
的文件,然后
取消链接

    $dir="./";
    $files=scandir($dir);
    if(is_array($files)){
        foreach($files as $file){
            if(is_file($dir.$file) && strpos($file,"JBB")!==false){
                unlink($dir.$file);
            }
        }
    }

您可以使用
scandir
在目录中循环,并使用
strpos
检查包含
JBB
的文件,然后取消链接

    $dir="./";
    $files=scandir($dir);
    if(is_array($files)){
        foreach($files as $file){
            if(is_file($dir.$file) && strpos($file,"JBB")!==false){
                unlink($dir.$file);
            }
        }
    }

我从没想过这个。非常感谢。我从没想过这个。非常感谢。