代码之家  ›  专栏  ›  技术社区  ›  Error 404

在文件中查找一个单词,然后将接下来的10个值放入数组

  •  2
  • Error 404  · 技术社区  · 8 年前

    我有一个文本文件,我想在数组中放入10个值,从我第一次看到特定的单词开始。假设文本包含:

    ...,sometext,moretext,wordddddd,867767,3468647,sometext,...
    

    wordddddd , array[0] 867767 array[1] 34686 等等我不需要数组中的逗号,语言是PHP。

    在我的尝试中,我试图扫描整个文件,如果我看到这个单词 然后做一个 for explode 把单词从逗号上分开。

    我用错误标记了这行

       <?php
        $id='wordddddd';
        $handle = fopen('file.txt', 'r');
        $valid = false; // init as false
        $arr=explode(",", $handle);// ERROR IN THIS LINE Warning: explode() expects parameter 2 to be string, resource given in 
        while (($buffer = fgets($handle)) !== false) {
            if (strpos($buffer, $id) !== false) {
                $valid = TRUE;
                $pos=strpos($buffer, $id);
                echo 'Its there in pos ',$pos;
                for($x=0;$x<=10;$x++){
                    echo $valid[$x+$pos+1];
    
                }
    
                break; // Once you find the string, you should break out the loop.
            }
        }
    
        fclose($handle);
        ?>
    
    4 回复  |  直到 8 年前
        1
  •  2
  •   Sheidin    8 年前
        $your_word = 'wordddddd';
    $number_of_items_to_limit = 10;
    $array = explode(",", file_get_contents('text.txt')); //Explode file content by ","
    $found = false; //Set dafault value for key word flag to false
    $array = array_filter( // remove all items without found flag
        $array,
        function($value) use (&$found,&$your_word) {
            if($value == $your_word) {
                $found = true; // set flag to found
            }
            if($found) {
                return true ; //return items that are coming after the flag
            } else {
                return false;
            }
        });
    $result = array_slice($array, 0, $number_of_items_to_limit); // return only first 10 items from array
    
        2
  •  2
  •   Ron Inbar    8 年前

    fgetcsv explode . 以下是手册页中的示例:

    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
    }
    
        3
  •  1
  •   user8461074 user8461074    8 年前
    $id='wordddddd';
    $fopen = fopen("file.txt", 'r');
    $content= htmlspecialchars(file_get_contents("file.txt"));//
    $split=explode(",", $content);
    $arr_len=count($split);
    $pos=0;
    $result=array();
    
    //finding the position of the word in the array
    foreach ($split as $x){
        if(stripos($id, $x)!==false) {
        break;}
        $pos++;
    }
    
    for($x=0;$x<10;$x++){
        $result[$x]=$split[$pos+1];
        $pos++;
    }
    
        4
  •  0
  •   Itzik Chaimov    8 年前

    错误是因为explode()不适用于$handle,但适用于不适用于case的字符串变量。

    推荐文章