php - 抓取给定关键字前后的 x 个单词?

标签 php mysql string code-snippets token

我怎样才能在 PHP 中获取字符串中给定关键字前后的 [x] 个单词?我正在尝试将针对关键字定制的 mysql 查询的结果标记为片段。

最佳答案

$string = 'This is a test string to see how to grab words from an arbitrary sentence. It\'s a little hacky (as you can see from the results) - but generally speaking, it works.';

echo $string,'<br />';

function getWords($string,$word,$before=0,$after=0) {
    $stringWords = str_word_count($string,1);
    $myWordPos = array_search($word,$stringWords);

    if (($myWordPos-$before) < 0)
        $before = $myWordPos;
    return array_slice($stringWords,$myWordPos-$before,$before+$after+1);
}

var_dump(getWords($string,'test',2,1));
echo '<br />';
var_dump(getWords($string,'this',2,1));
echo '<br />';
var_dump(getWords($string,'sentence',1,3));
echo '<br />';
var_dump(getWords($string,'little',2,2));
echo '<br />';
var_dump(getWords($string,'you',2,2));
echo '<br />';
var_dump(getWords($string,'results',2,2));
echo '<br />';
var_dump(getWords($string,'works',2,2));

echo '<hr />';


function getWords2($string,$word,$before=0,$after=0) {
    $stringWords = str_word_count($string,1);
    $myWordPos = array_search($word,$stringWords);
    $stringWordsPos = array_keys(str_word_count($string,2));

    if (($myWordPos+$after) >= count($stringWords))
        $after = count($stringWords) - $myWordPos - 1;
    $startPos = $stringWordsPos[$myWordPos-$before];
    $endPos = $stringWordsPos[$myWordPos+$after] + strlen($stringWords[$myWordPos+$after]);

    return substr($string,$startPos,$endPos-$startPos);
}

echo '[',getWords2($string,'test',2,1),']<br />';
echo '[',getWords2($string,'this',2,1),']<br />';
echo '[',getWords2($string,'sentence',1,3),']<br />';
echo '[',getWords2($string,'little',2,2),']<br />';
echo '[',getWords2($string,'you',2,2),']<br />';
echo '[',getWords2($string,'results',2,2),']<br />';
echo '[',getWords2($string,'works',1,3),']<br />';

但是如果这个词出现多次,你希望发生什么?或者如果这个词没有出现在字符串中?

编辑

getWords2 的扩展版本,最多可返回关键字出现的设定次数

$string = 'PHP is a widely-used general-purpose scripting language that is especially suited for Web development. The current version of PHP is 5.3.3, released on July 22, 2010. The online manual for PHP is an excellent resource for the language syntax and has an extensive list of the built-in and extension functions. Most extensions can be found in PECL. PEAR contains a plethora of community supplied classes. PHP is often paired with the MySQL relational database.';

echo $string,'<br />';

function getWords3($string,$word,$before=0,$after=0,$maxFoundCount=1) {
    $stringWords = str_word_count($string,1);
    $stringWordsPos = array_keys(str_word_count($string,2));

    $foundCount = 0;
    $foundInstances = array();
    while ($foundCount < $maxFoundCount) {
        if (($myWordPos = array_search($word,$stringWords)) === false)
            break;
        ++$foundCount;
        if (($myWordPos+$after) >= count($stringWords))
            $after = count($stringWords) - $myWordPos - 1;
        $startPos = $stringWordsPos[$myWordPos-$before];
        $endPos = $stringWordsPos[$myWordPos+$after] + strlen($stringWords[$myWordPos+$after]);

        $stringWords = array_slice($stringWords,$myWordPos+1);
        $stringWordsPos = array_slice($stringWordsPos,$myWordPos+1);

        $foundInstances[] = substr($string,$startPos,$endPos-$startPos);
    }
    return $foundInstances;
}

var_dump(getWords3($string,'PHP',2,2,3));

关于php - 抓取给定关键字前后的 x 个单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3684754/

相关文章:

MySQL 统计记录并使用权重

mysql - 使用 MySQL CASE 表达式进行聚合

PHP shell_exec 停止工作

php - MySQL 查询不打印所有表数据

mysql - 使用 DISTINCT 查询时列的顺序错误

java - Str.lastIndexOf ("\")给出错误

python - 算法问题,python字符串,不知道

string - 如何在不添加空格的情况下连接 TCL 中的字符串

php - Codeigniter 字限制器关闭 html 标签

php - 无法更新 Google 云端硬盘文件