【问题标题】:Sorting files in an array by the ocurrences of a word in it, php根据其中出现的单词对数组中的文件进行排序,php
【发布时间】:2017-11-28 05:20:20
【问题描述】:

我正在制作一个搜索栏,用于搜索目录中已搜索单词的文件,然后我希望将其按顺序添加到数组中,其中一个单词的询问次数多于询问单词的次数少。 我正在使用 PHP,这是我的代码:

<?php
    
    if(isset($_POST['busqueda'])){
        $variable = utf8_encode($_POST['busqueda']);
    }
    $Array1 = array();

    foreach(glob("*.txt") as $filename) {
        $contents = file_get_contents($filename);
        if (strpos($contents, $variable)){
             $Array1[] = $filename;
      }
    }

我不知道该怎么做,我认为我应该使用substr_count(file_get_contents($Array1[$position1]))或类似的东西,但我不确定如何制作排序系统,有人可以帮助我!

print_r($Array1);

for($var1=0; $var1<sizeof($Array1); $var1++){
       echo "times on the file: ".$Array1[$var1]."<br>";
        echo substr_count(file_get_contents($Array1[$var1]));
  }
?>

【问题讨论】:

  • 如果你可以通过 exec 或类似的函数运行 bash 命令,你可以使用tr ' ' '\n' &lt; THEFILEPATH | grep THEWORD | wc -l 来快速计算 THEWORD 恰好在 THEFILEPATH 文件中出现的所有次数。

标签: php arrays sorting repeat


【解决方案1】:

您可以使用 substr_count 本身。然后你需要使用arsort对数组进行排序。

$Array1 = array();
foreach (glob("*.txt") as $filename) {
    $contents = file_get_contents($filename);
    if ( ($count = substr_count($contents, $variable)) ) {
        $Array1[$filename] = $count;
    }
}

arsort($Array1) ;

print_r($Array1);

foreach ($Array1 as $file => $count) {
    echo "times on the file($file): $count <br>";
}

【讨论】:

  • Array ( [0] => 59 [1] => 50 [2] => 50 [3] => 50 [4] => 50 [5] => 6 ) 次文件(0):文件59次(1):文件50次(2):文件50次(3):文件50次(4):文件50次(5): 6 它没有将 $filename 保存在数组中 :( (在 foreach 中我尝试用 fopen 打开 $file,所以我可以逐行读取以显示一些内容,但它说没有名为 0,1,2 的文件,...)
  • 你用的是rsort还是arsort?
  • 忘记打字是个失误
  • 它有一种简单的方法来应用过滤吗?我的意思是在单击复选框(或按钮)时只显示超过 x 字数的元素?
【解决方案2】:

Bash(至少在 Linux 和 Mac 操作系统上可用)使完成任务变得非常容易,因为您可以通过 PHP 的 exec 函数调用命令,前提是它没有被管理员禁用。如果您使用的是 Windows,那么这可能行不通,但大多数人都将 Linux 用于生产环境,所以我认为这个答案值得发布。

以下函数取自 CodeIgniter 的文件助手,仅用于从指定目录获取文件名数组。如果您不需要这样的函数,因为您从其他地方获取文件名,请注意此函数可以包含每个文件的完整文件路径,这就是我使用它的原因。

function get_filenames($source_dir, $include_path = FALSE, $_recursion = FALSE)
{
    static $_filedata = array();

    if ($fp = @opendir($source_dir))
    {
        // reset the array and make sure $source_dir has a trailing slash on the initial call
        if ($_recursion === FALSE)
        {
            $_filedata = array();
            $source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
        }

        while (FALSE !== ($file = readdir($fp)))
        {
            if (@is_dir($source_dir.$file) && strncmp($file, '.', 1) !== 0)
            {
                get_filenames($source_dir.$file.DIRECTORY_SEPARATOR, $include_path, TRUE);
            }
            elseif (strncmp($file, '.', 1) !== 0)
            {
                $_filedata[] = ($include_path == TRUE) ? $source_dir.$file : $file;
            }
        }
        return $_filedata;
    }
    else
    {
        return FALSE;
    }
}

现在我可以轻松获取文件名数组,我会这样做:

/**
 * Here you can see that I am searching 
 * all of the files in the script-library 
 * directory for the word "the"
 */
$searchWord = 'the';
$directory = '/var/www/htdocs/script-library';

$filenames = get_filenames(
    $directory,
    TRUE    
);

foreach( $filenames as $file )
{
    $counts[$file] = exec("tr ' ' '\n' < " . $file . " | grep " . $searchWord . " | wc -l");
}

arsort( $counts );

echo '<pre>';
print_r( $counts );
echo '</pre>';

有关其工作原理的详细说明,请参阅:https://unix.stackexchange.com/questions/2244/how-do-i-count-the-number-of-occurrences-of-a-word-in-a-text-file-with-the-comma

我在本地测试了这段代码,效果很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    相关资源
    最近更新 更多