1

検索語を含む文を取得したい。私はこれを試しましたが、正しく動作させることはできません。

$string = "I think instead of trying to find sentences, I'd think about the amount of 
context around the search term I would need in words. Then go backwards some fraction of this number of words (or to the beginning) and forward the remaining number 
of words to select the rest of the context.";

$searchlocation = "fraction";

$offset = stripos( strrev(substr($string, $searchlocation)), '. ');
$startloc = $searchlocation - $offset;
echo $startloc;
4

3 に答える 3

3

すべての文を取得できます。

これを試して:

$string = "I think instead of trying to find sentences, I'd think about the amount of 
context around the search term I would need in words. Then go backwards some fraction of this number of words (or to the beginning) and forward the remaining number 
of words to select the rest of the context.";

$searchlocation = "fraction";

$sentences = explode('.', $string);
$matched = array();
foreach($sentences as $sentence){
    $offset = stripos($sentence, $searchlocation);
    if($offset){ $matched[] = $sentence; }
}
var_export($matched);
于 2012-09-02T21:06:39.993 に答える
2

array_filter関数を使用する

$sentences = explode('.', $string);
$result = array_filter(
    $sentences, 
    create_function('$x', "return strpos(\$x, '$searchlocation');"));

注:の2番目のパラメーターの二重引用符create_functionが必要です。

匿名関数をサポートしている場合は、これを使用できます、

$result = array_filter($sentences, function($x) use($searchlocation){
        return strpos($x, $searchlocation)!==false;
});
于 2012-09-02T21:14:38.023 に答える
1

で文字列を逆にするので、代わりにstrrev()が見つかります。[space]..[space]

于 2012-09-02T21:06:45.720 に答える