PHP如何提取给定字符串的一部分?

时间:2010-10-08 12:02:45

标签: php regex extract

我正在为我的网站编写搜索引擎,需要使用给定的关键字提取文本块,并为搜索结果列表提取几个字。 我结束了这样的事情:


/**
 * This function return part of the original text with
 * the searched term and few words around the searched term
 * @param string $text Original text
 * @param string $word Searched term
 * @param int $maxChunks Number of chunks returned
 * @param int $wordsAround Number of words before and after searched term
 */
public static function searchTerm($text, $word=null, $maxChunks=3, $wordsAround=3) {
        $word = trim($word);
        if(empty($word)) {
            return NULL;
        }
        $words = explode(' ', $word); // extract single words from searched phrase
        $text  = strip_tags($text);  // clean up the text
        $whack = array(); // chunk buffer
        $cycle = 0; // successful matches counter
        foreach($words as $word) {
            $match = array();
            // there are named parameters 'pre', 'term' and 'pos'
            if(preg_match("/(?P\w+){0,$wordsAround} (?P$word) (?P\w+){0,$wordsAround}/", $text, $match)) {
                $cycle++;
                $whack[] = $match['pre'] . ' ' . $word . ' ' . $match['pos'];
                if($cycle == $maxChunks) break;
            }
        }
        return implode(' | ', $whack);
    }
此功能不起作用,但您可以看到基本的想法。欢迎任何有关如何改进正则表达式的建议!

2 个答案:

答案 0 :(得分:1)

从不从不将用户内容注入RegEx的模式而不使用preg_quote来清理输入:

http://us3.php.net/manual/en/function.preg-quote.php

答案 1 :(得分:1)

为什么在这里重新发明轮子不会谷歌有最好的搜索引擎我会看他们appliance