English 中文(简体)
利用关键词在PHP中填充阵列?
原标题:Filter array in PHP using keywords?

What could I add to the following code to filter the returned results using keywords in an array?

发出关键词并发出单调呼的法典位于贾瓦斯里。

下面的代码开立档案,使用请求使用一个点子,开立档案查找点,并从点子到档案末检索所有记录线。 然后,它形成一个JSON物体,然后送回Javacript。

function tail(){
    $keywords = json_decode($_REQUEST[ keywords ]);

    $file = "/path/to/the/log.log";
    $handle = fopen($file, "r");
    clearstatcache();       

    if ($_REQUEST[ pointer ] ==   ) {
        fseek($handle, -1024, SEEK_END);
    } else {
        fseek($handle, $_REQUEST[ pointer ]);
    }

    while ($buffer = fgets($handle)) { 
        $log .= $buffer . "<br />
";
    } 



    $output = array("pointer" => ftell($handle), "log" => $log);
    fclose($handle);

    echo json_encode($output);
}

请告诉我,我如何能够利用关键词阵列中的关键词对检索的数据进行过滤。

最佳回答

您可按每一空间分界线,并对照所提供关键词核对每一字:

while ($buffer = fgets($handle)) { 
  $words = explode(   , $buffer);
  foreach ($words as $word) {
    if (in_array($word, $keywords)) {
      $log .= $buffer . "<br />
";
      break;
    }
  }
}

或对照行文核对每一关键词:

while ($buffer = fgets($handle)) { 
  foreach ($keywords as $keyword) {
    if (strstr($buffer, $keyword)) {
      $log .= $buffer . "<br />
";
      break;
    }
  }
}
问题回答

改写:

while ($buffer = fgets($handle)) { 
    foreach ($keywords as $kw) { // Loop keywords
        if (strpos($buffer, $kw) !== FALSE) { // Search for this keyword
            // If we get here, we found a keyword
            $log .= $buffer . "<br />
";
            break;
        }
    }
} 

如果你想以对个案敏感的方式加以匹配,你可以使用<代码>stripos(。





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签