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

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

问题描述:

i need to extract and show some words before and after a query word, something like google search results, for example:

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

i searched google an SO but didn't find a clear answer or maybe my php knowledge was not enough! is there a workable and easy-to-use function to do this job?

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

  $ str =“hi user!welcome 到新的PHP开源世界,我们正在努力学习你的东西!“; 
 $ query =”new php“; 
 $ result =”...欢迎来到新的php开源......“; 
   pre> 
 
 

我搜索了谷歌一个SO但没有找到一个明确的答案或者我的PHP知识还不够! 有一个可行且易于使用的功能要做 这份工作? p> div>

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

Output is: user! welcome to new php open source world, ($numOfWordToAdd = 3)

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

This will return the same result from the same string and query you gave. If the before or after length starts or ends (respectively) in the middle of a word, it will continue until it completes the word before it stops.

Assuming a "word" is any series of non-whitespace characters, the following will extract 3 words on either side of new php out of the string $subject, but accept less if necessary:

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

Change the 3s to any number you like.

Here is an working example I thing that it is clear what I did and how:

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

?>

If you have some questions feel free to ask...

I used the following function with explode:

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