2010-09-10 59 views

回答

5
$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)); 
+0

嗨,馬克的感謝,如果這個詞出現多次,那麼我想拼湊來自倍數的片段,例如多達3次迭代。所以最終的結果可能是這樣的:這是關於KEYWORD是關於......這是另一個關鍵字拼圖塊......這將是KEYWORD第三塊 – 2010-09-10 14:44:15

+0

我想要做的是提供足夠的細節,結果是,沒有太相似的對方。 – 2010-09-10 14:46:14

+0

這就像一個魅力,謝謝很多馬克:) – 2010-09-12 14:28:49

相關問題