在单词之前和之后提取字符串的一部分

时间:2012-09-27 19:55:29

标签: php regex string search

我需要提取并在查询词之后显示 之前的之后的等词语,例如Google搜索结果,例如:

$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 ...";

我搜索谷歌一个SO但没有找到一个明确的答案或者也许我的PHP知识还不够! 是否有一个可行且易于使用的功能来完成这项工作?

5 个答案:

答案 0 :(得分:5)

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

答案 1 :(得分:2)

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

<?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;

?>

如果您有一些问题可以随意提问......

答案 2 :(得分:1)

$result = preg_replace('/(.+)?([^\s]+.{10}'.$query.'.{10}[^\s]+)(.+)?/', '... $2 ...', $str);

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

答案 3 :(得分:0)

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

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

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

答案 4 :(得分: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;
}