亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在php中從包含特定單詞的數組中去除文件

如何在php中從包含特定單詞的數組中去除文件

PHP
蕭十郎 2022-09-30 16:24:13
我在文件夾中有一些.txt文件。每個文件有 6 行。我的文件看起來像這樣:messagesid_20197456 // identityFriends //categoryTest // title10 Feb 2020 22:28 // dateJohn // writerLorum ipsum.... // message類別的名稱始終在第二行我總共有5個文件:4個類別,1個類別?,F在我想去掉具有類別的文件FriendsOffsideOffside這就是我到目前為止所擁有的,以捕獲帶有類別的文件:Offside$filterthis = strtolower('Offside');$newslist = array();$files = glob("messages/*.txt"); // Specify the file directory by extension (.txt)foreach($files as $file) { // Loop through the files in the directory       $handle = @fopen($file, "r");    if ($handle) {        $lines = file($file); //file into an array        $buffer = $lines[1]; // grab category line        if(strpos(strtolower($buffer), $filterthis) !== FALSE) { // strtolower; search word not case sensitive                  $newslist[] = $file; // The filename of the match                // below the file which has Offside category                print_r($newslist); // outputs: Array ( [0] => messages/id_20200210222825.txt )                         }        fclose($handle);    }}為了輸出所有文件,我使用一個foreach循環:foreach($newslist as $file) {    $lines = file($file, FILE_IGNORE_NEW_LINES); // filedata into an array    $file_id = $lines[0]; // file id    $news_category = $lines[1]; //  news category    $news_title = $lines[2]; //  news title    $news_date = $lines[3]; // news date    $news_author = $lines[4]; //  author name    $news_message = $lines[5]; // news message    fclose($fh);    // all the echos's come here...}我的問題:如何過濾在前程中沒有類別的文件?所以前期應該輸出所有的文件,除了那些有類別的文件?OffsideOffside
查看完整描述

1 回答

?
波斯汪

TA貢獻1811條經驗 獲得超4個贊

array_diff將完成這項工作

抓取文件夾中的所有文件:messages

// read all files in messages folder

$dir = 'messages/';

if ($dh = opendir($dir)) {

    while(($file = readdir($dh))!== false){

        if ($file != "." && $file != "..") { // This line strips out . & ..

            $newslist[] = $dir.$file;                       

        }       

    }

}

closedir($dh);

現在過濾哪個類別中的文件越位(你已經這樣做了)


// Strip file(s) with category Offside  

$strip_cat = strtolower('Offside');

$offside_array = array();


$files = glob("messages/*.txt"); // Specify the file directory by extension (.txt)


foreach($files as $file) { // Loop through the files in the directory   


    $handle = @fopen($file, "r");


    if ($handle) {


        $lines = file($file); //file into an array


        $buffer = $lines[1]; // grab category line


        if(strpos(strtolower($buffer), $strip_cat) !== FALSE) { // strtolower; search word not case sensitive   


                $offside_array[] = $file; // The filename of the match(es)


        }

        fclose($handle);

    }

}

現在比較 2 個數組:


// compare the arrays and strip out the files which contain cat Offside

$filtered_newslist = array_diff($newslist, $offside_array);

$filtered_newslist 是您的新數組,其中包含除具有類別的文件之外的所有文件Offside


您的前循環:


foreach($filtered_newslist as $file) {

    $lines = file($file, FILE_IGNORE_NEW_LINES); // filedata into an array


    $file_id = $lines[0]; // file id

    // and so on...

}


查看完整回答
反對 回復 2022-09-30
  • 1 回答
  • 0 關注
  • 93 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號