PHP-数组(文本文件)..或其他内容中的多个搜索查询(从表单) [英] PHP - multiple search queries (from form) in array (from textfile).. or something

查看:90
本文介绍了PHP-数组(文本文件)..或其他内容中的多个搜索查询(从表单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道该怎么称呼..总之:

Didn't know exactly what to call this.. Anyways:

我有一个文本文件,由以下内容组成:

I have a text-file consisting of:

something;something2;something3;something4\n

我使用常规的html形式进行用户输入. 用户输入= $ search. 我将此发送到我的搜索功能:

I use a regular html-form for userinput. Userinput = $search. I send this to my search-function:

function search($search) {
$lin = file('text.txt');
$lines = array_map('strtolower', $lin);
$found = false;
foreach($lines as $line) {
if(strpos($line, $search) !== false) {
$found = true;
$line = explode(";",$line);
$line[1] = ucwords($line[1]);
$line[2] = ucwords($line[2]);
$line[3] = strtoupper($line[3]);
echo "$hit[0] $hit[1] etc";
}
}
if(!$found) {
echo "No matches";
}
}

如果用户仅输入一个单词,这将很好地工作.假设文件的内容是: foo; bar; chuck; hank;

This works fine if the user only inputs one words. Let's say the content of the file is: foo;bar;chuck;hank;

我希望用户能够搜索"foo chuck"并仍然得到结果.即使只有一个单词匹配,也应回显该行(例如,"foo carl"仍会输出整行).

I would like the user to be able to search "foo chuck" and still get a result. Even if only one word match, the line should be echoed (ex "foo carl" still outputs the entire line).

但是再说一次,如果有一个"foo; bar"和一个"foo; carl",我不希望用户搜索"foo carl"时显示foo bar.

But then again, if there is a "foo;bar" and a "foo;carl", I don't want foo bar to show if the user searches for "foo carl"..

有什么提示吗?

推荐答案

您可以获取用户输入,将其拆分为单词,以与规范化文件内容相同的方式对其进行规范化,然后将其放入数组$input

You could take the user input, split it into words, normalize them in the same manner as you normalize the file contents and put them into an array $input.

然后,对于文件内容中的每一行,再次执行完全相同的过程,最后以数组$line结尾.

Then, for each row in the file contents follow the exact same procedure again ending up with an array $line.

根据您的描述,当$input中的所有单词也都出现在$line中时,就会发生搜索命中,其表达方式可能是:

Based on your description a search hit occurs when all the words inside $input are also present inside $line, which could be expressed as:

$hit = count(array_intersect($input, $line)) == count($input);

别忘了使用array_unique修剪" $input$line来涵盖搜索词出现多次的情况.

Don't forget to "trim" $input and $line using array_unique to cover the case where a search term appears more than once.

示例代码:

function search($search) {
  // break input into words and normalize
  // explode() is maybe not going to cut it in production, but it's OK here
  $search = array_unique(explode(' ', strtolower($search)));
  $terms = count($search);

  $lines = file('text.txt');
  $found = false;
  foreach($lines as $line) {
    // same treatment as for input
    $line = array_unique(explode(' ', strtolower($line)));

    if(count(array_intersect($search, $line)) == $terms) {
      $found = true;
      $line = explode(";",$line);
      $line[1] = ucwords($line[1]);
      $line[2] = ucwords($line[2]);
      $line[3] = strtoupper($line[3]);
      echo "$hit[0] $hit[1] etc";
    }
  }
}

这篇关于PHP-数组(文本文件)..或其他内容中的多个搜索查询(从表单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆