【问题标题】:PHP - Show searching Word with 5 surrounding WordsPHP - 用 5 个周围的词显示搜索词
【发布时间】:2014-03-31 13:27:16
【问题描述】:

我是新来的。我对一些 PHP 有疑问。我目前正在做一个项目。现在有一个问题。

所以,首先:我的“愿望”是什么: 有一个文本文件,它包含如下内容:

John Brown: Lives in New York, married, have 3 sons and love playing football

现在我需要一个 PHP 代码,它读取和搜索文本文件,但只显示特殊搜索词,搜索词周围有 5 个词,所以结果应该是这样的: 我正在搜索儿子,结果应该是: John Brown: Have 3 sons and love playing。 编辑:忘了说,John Brown 这个名字应该保留在搜索结果中。

请帮助我。 对不起我的英语不好,住在德国:)

这是我迄今为止尝试过的:

<?php
$search = 'sons';
$lines = file('file.txt'); 
// Store true when the text is found 
$found = false;
foreach($lines as $line) { 
  if(strpos($line, $search) !== false) { $found = true; echo $line; } 
} 
// If the text was not found, show a message 
if(!$found) { echo 'No match found'; }
?>

【问题讨论】:

  • 您的英语很好,但我们是否喜欢坐在这里从头到尾提供免费代码? :)
  • 我从来没有这么说过! ;)
  • 但是你期待它,对吧?请意识到您必须自己做出一些努力。
  • 当然,我已经为自己尝试过……但没有得到我应该得到的结果。我试过这样:
  • 看到了吗?如果您已经发布了代码,我们可以跳过所有这些。无论如何,添加标签并将代码发布到问题中。这样你的问题会被更好地注意到。

标签: php search text text-files words


【解决方案1】:

这里,只是为了笑,是一个循环遍历字符串计数空间而不是爆炸和内爆的解决方案:

function context_find($haystack, $needle, $context) {
    $haystack=' '.$haystack.' ';
    if ($i=strpos($haystack, $needle)) {
        $start=$i;
        $end=$i;
        $spaces=0;

        while ($spaces < ((int) $context/2) && $start > 0) {
            $start--;
            if (substr($haystack, $start, 1) == ' ') {
                $spaces++;
            }
        }

        while ($spaces < ($context +1) && $end < strlen($haystack)) {
            $end++;
            if (substr($haystack,$end,1) == ' ') {
                $spaces++;
            }
        }

        while ($spaces < ($context +1) && $start > 0) {
            $start--;
            if (substr($haystack, $start, 1) == ' ') {
                $spaces++;
            }
        }

        return(trim(substr($haystack, $start, ($end - $start))));
    } else {
        return false;
    }
}

例如:

$h="Twas brillig and the slithy toves did gyre and gimbel in the wabe";
$n="toves";
$c="5";

print context_find($h, $n, $c)."\n";

返回:

the slithy toves did gyre

此外,即使搜索词离开头或结尾太近,它也会尝试返回适量的上下文:

$h="Twas brillig and the slithy toves did gyre and gimbel in the wabe";
$n="brillig";
$c="5";

print context_find($h, $n, $c)."\n";

返回:

Twas brillig and the slithy

甚至:

$h="Twas brillig and the slithy toves did gyre and gimbel in the wabe";
$n="wabe";
$c="5";

返回:

and gimbel in the wabe

当然,这对循环输入文件等没有任何作用,其他示例就足够了。

【讨论】:

    【解决方案2】:

    对我来说,这听起来像是正则表达式处理得很好的问题。

    function find_words($haystack, $needle) {
        $regex = '%\w+\s\w+\s' . preg_quote($needle) . '\s\w+\s\w+%';
    
        if (preg_match($regex, $haystack, $matches)) {
            return $matches[0];
        } else {
            return false;
        }
    }
    
    $s = 'John Brown: Lives in New York, married, have 3 sons and love playing football';
    $search = 'sons';
    var_dump(find_words($s, $search));
    

    \w+ 是一个或多个单词字符,\s 是一个空格,中间是您要搜索的单词。很容易匹配更多的单词或其他字符,或者进行不完全匹配的单词,这取决于您的需要。

    【讨论】:

      【解决方案3】:

      这段代码只是我如何做的一个例子,它还没有完成。

        function search($query,$str){
           $words = explode(' ',$str);
           $index = array_search($query, $words);
           return implode(' ', array_slice($words, $index-2, 6)); // sanitate "length of the array" vs $index
        }
        $file = file_get_contents('data');
      
        echo  search('sons',$file);
      

      此函数将返回在搜索词周围包含 2 个词的字符串。

      请记住,$file 可以包含多个搜索词,并且 $index-2 可以小于 $words 数组的长度。

      如果您的数据文件在 1 行包含 1 个句子,您可以这样使用它

        $file = file('data');
        foreach ($file as $line){
          echo search('sons',$line)."\n";
        }
      

      另外,如果每一行的名字被定义为“$Name: $sentence”,使用这个函数来查找它

        function findName($str){
          $name = explode(':',  $str);
          return $name[0];
        }
      

      组合起来是这样的:

        function search($query,$str){
           $words = explode(' ',$str);
           $index = array_search($query, $words);
           if ($idex === false){
             return false;
           }
           else{
           return implode(' ', array_slice($words, $index-2, 6)); // sanitate "length of the array" vs $index
           }
        }
        function findName($str){
          $name = explode(':',  $str);
          return $name[0];
        }
      
        $file = file('data');
        foreach ($file as $line){
          $string =  search('sons',$line);
          if ($string !== false){
            echo findName($line).': '.$string."\n";
          }
        }
      

      您应该能够自己完成代码。如果没有,请告诉我。

      【讨论】:

      • 嗯,我不知道,但它不起作用.. 结果是这样的:有 3 个
      • 对不起,我编辑了代码。查看array_slice函数的变化
      猜你喜欢
      • 1970-01-01
      • 2012-08-26
      • 1970-01-01
      • 2012-10-15
      • 2011-02-09
      • 2010-10-11
      • 1970-01-01
      • 2014-02-13
      • 1970-01-01
      相关资源
      最近更新 更多