Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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_Search Engine - Fatal编程技术网

使用php获取类似搜索引擎的搜索正文片段

使用php获取类似搜索引擎的搜索正文片段,php,search,search-engine,Php,Search,Search Engine,我让我的网站搜索引擎完全按照我想要的方式运行,除了一个令人困扰的问题:我希望能够显示所有搜索引擎所显示的搜索主体类型,它们在我的搜索结果中突出显示包含你的搜索词的1到3个句子 我的Google Foo在这一点上并不强大,所以我希望有人能让我使用现有的解决方案。你是说搜索突出显示: str_replace( $searchTerm, '<span class="searchHighlight">'.$searchTerm.'</span>', $

我让我的网站搜索引擎完全按照我想要的方式运行,除了一个令人困扰的问题:我希望能够显示所有搜索引擎所显示的搜索主体类型,它们在我的搜索结果中突出显示包含你的搜索词的1到3个句子


我的Google Foo在这一点上并不强大,所以我希望有人能让我使用现有的解决方案。

你是说搜索突出显示:

str_replace(
    $searchTerm,  
    '<span class="searchHighlight">'.$searchTerm.'</span>',
    $searchString);
stru\u替换(
$searchTerm,
“.$searchTerm.”,
$searchString);
您需要在纯文本上执行此操作,否则您可能会遇到文章中提到的一些复杂问题


您可以尝试基于Javascript的方法,但基于PHP/HTML的方法更容易实现。

如果您不想像battal建议的那样突出显示关键字,并且想要剪切相关段落/内容,我会这样做:

$snippets = array();
foreach ($matches as $i => $match) {
    $pos = strpos($match, $searchTerm);

    $buffer = 30; // characters before and after the search term is found
    // start index - 0 or 30 characters before instance of search term
    $start = ($pos - $buffer >= 0) ? $pos - $buffer : 0;
    // end index - 30 characters after instance of search term or the length of the match
    $end = $start + strlen($searchTerm) + $buffer;
    $end = ($end >= strlen($match)) ? strlen($match) : $end;

    $snippets[$i] = substr($match, $start, $end);
}

如何保存结果?关于搜索结果及其来源有哪些信息?您使用的是哪种搜索引擎?