Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP搜索和显示带有关键字的结果_Php_Search_Scripting - Fatal编程技术网

PHP搜索和显示带有关键字的结果

PHP搜索和显示带有关键字的结果,php,search,scripting,Php,Search,Scripting,我想做的是,我有一个如下所示的文件: Blue = 1 = No = 20, Arctic color = 2 = No = 20, Pink = 3 = No = 20, Arctic blue color = 4 = No = 20, Red = 5 = No = 20, Orange = 6 = No = 20, 假设用户在表单中键入短语“北极色”。我希望我的代码能够显示所有包含短语“北极色”的行。因此,代码应该响应: Arctic color = 2 = No = 20, Arctic

我想做的是,我有一个如下所示的文件:

Blue = 1 = No = 20,
Arctic color = 2 = No = 20,
Pink = 3 = No = 20,
Arctic blue color = 4 = No = 20,
Red = 5 = No = 20,
Orange = 6 = No = 20,
假设用户在表单中键入短语“北极色”。我希望我的代码能够显示所有包含短语“北极色”的行。因此,代码应该响应:

Arctic color = 2 = No = 20,
Arctic blue color = 4 = No = 20,

但是,我如何才能做到这一点?任何帮助都将不胜感激

我建议将结构化数据存储在某种数据库中。例如,mysql。 然后您可以发出一个类似于的查询。如果左边有一个通配符,这将执行完整的表扫描,这可能不是问题,具体取决于您拥有的行数

如果需要更高性能的解决方案,则需要将数据转换为更易于搜索的格式


如果您只想解决您的确切问题:

要查找包含中所有单词的行,请执行以下操作:

$query = 'arctic color';

$terms = array_filter(array_map('trim', explode(" ", $query)));
$results = array();

foreach(file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
    $found = true;

    foreach($terms as $term) {
        if(strpos($line, $term) === false) {
            $found = false;
            break;
        }
    }

    if($found) {
        $results[] = $line;
    }
}

print_r($results);
$query = 'arctic color';

$terms = array_filter(array_map('trim', explode(" ", $query)));
$results = array();

foreach(file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
    $found = false;

    foreach($terms as $term) {
        if(strpos($line, $term) !== false) {
            $found = true;
            break;
        }
    }

    if($found) {
        $results[] = $line;
    }
}

print_r($results);
要查找包含中某些单词的行,请执行以下操作:

$query = 'arctic color';

$terms = array_filter(array_map('trim', explode(" ", $query)));
$results = array();

foreach(file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
    $found = true;

    foreach($terms as $term) {
        if(strpos($line, $term) === false) {
            $found = false;
            break;
        }
    }

    if($found) {
        $results[] = $line;
    }
}

print_r($results);
$query = 'arctic color';

$terms = array_filter(array_map('trim', explode(" ", $query)));
$results = array();

foreach(file('file.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
    $found = false;

    foreach($terms as $term) {
        if(strpos($line, $term) !== false) {
            $found = true;
            break;
        }
    }

    if($found) {
        $results[] = $line;
    }
}

print_r($results);

那么,您想获取查询的第一个单词并搜索它吗?搜索词是否需要位于行的开头?来吧,给我们更多的细节。我想进行完整的搜索查询,看看查询中的任何单词是否在文件的任何一行中找到。如果是,我希望脚本回显包含查询中任何一个单词的行。这种结构化数据通常保存在SQL数据库中,而不是文本文件中。这使得复杂的查询变得更加简单和高效。看起来就像