2

我需要在查询词之前之后提取并显示一些词,例如谷歌搜索结果,例如:

$str = "hi user! welcome to new php open source world, we are trying to learn you something!";
$query = "new php";
$result = "... welcome to new php open source ...";

我在 google 上搜索了一个 SO,但没有找到明确的答案,或者我的 php 知识还不够!是否有一个可行且易于使用的功能来完成这项工作?

4

5 回答 5

6
function yourFuncName($str, $query, $numOfWordToAdd) {
    list($before, $after) = explode($query, $str);

    $before = rtrim($before);
    $after  = ltrim($after);

    $beforeArray = array_reverse(explode(" ", $before));
    $afterArray  = explode(" ", $after);

    $countBeforeArray = count($beforeArray);
    $countAfterArray  = count($afterArray);

    $beforeString = "";
    if($countBeforeArray < $numOfWordToAdd) {
        $beforeString = implode(' ', $beforeArray);
    }
    else {
        for($i = 0; $i < $numOfWordToAdd; $i++) {
            $beforeString = $beforeArray[$i] . ' ' . $beforeString; 
        }
    }

    $afterString = "";
    if($countAfterArray < $numOfWordToAdd) {
        $afterString = implode(' ', $afterArray);
    }
    else {
        for($i = 0; $i < $numOfWordToAdd; $i++) {
            $afterString = $afterString . $afterArray[$i] . ' '; 
        }
    }

    $string = $beforeString . $query . ' ' . $afterString;

    return $string;
}

输出为: user! welcome to new php open source world, ( $numOfWordToAdd = 3 )

于 2012-09-27T20:27:31.620 回答
3

这是一个工作示例,我很清楚我做了什么以及如何做:

<?php

$str = "hi user! welcome to new php open source world, we are trying to learn you something!";
$query = "new php";    

$expl = explode($query, $str);

    // items on the left side of middle string
    $expl_left = explode(" ", $expl[0]);

    $left_cnt = count($expl_left);

    $new_left = $expl_left[$left_cnt-3] . " " . $expl_left[$left_cnt-2];

    // items on the right side of middle string
    $expl_right = explode(" ", $expl[1]);

    $new_right = $expl_right[1] . " " . $expl_right[2];

    // new string formated
    $new = "... " . $new_left . " " . $query . " " . $new_right . " ...";

print $new;

?>

如果您有一些问题,请随时问...

于 2012-09-27T20:17:01.093 回答
1
$result = preg_replace('/(.+)?([^\s]+.{10}'.$query.'.{10}[^\s]+)(.+)?/', '... $2 ...', $str);

这将从您提供的相同字符串和查询返回相同的结果。如果之前或之后的长度(分别)在单词的中间开始或结束,它将继续直到它在停止之前完成单词。

于 2012-09-27T20:04:09.853 回答
0

假设“单词”是任何一系列非空白字符,以下将new php在字符串的任一侧提取 3 个单词$subject,但如有必要则接受更少的单词:

if (preg_match('/(?:\S+\s+){1,3}new php(?:\s+\S+){1,3}/', $subject, $regs)) {
    $result = $regs[0];
}

将 s 更改为3您喜欢的任何数字。

于 2012-09-27T20:06:55.433 回答
0

我使用了以下功能和爆炸:

public static function returnSearch($query, $str, $wordcount) {
    $explode = explode($query, $str);
    $result = null;
    //if explode count is one the query was not found
    if (count($explode) == 1) {
        $result = implode(' ', array_slice(str_word_count($explode[0], 2), -$wordcount, $wordcount)) . " ";
    }
    //if explode count is more than one the query was found at least one time
    if (count($explode) > 1) {
        //check for if the string begins with the query
        if (!empty($explode[0])) {
            $result =  "..." . implode(' ', array_slice(str_word_count($explode[0], 2), -$wordcount, $wordcount)) . " ";
        }
        $result = $result . $query;
        if (!empty($explode[1])) {
            $result = $result . " " . implode(' ', array_slice(str_word_count($explode[1], 2), 0, $wordcount)) . "...";
        }
    }
    //return result
    return $result;
}
于 2016-06-13T16:59:54.537 回答