필자의 초기 생각은 공백 문자를 폭발시킨 다음 단어가 결과 배열에 있는지 확인하는 것입니다. 당연히 고려해야 할 배열에 누 구두점이 누출 될 수 있습니다.
또 다른 아이디어는 단어의 strpos
을 확인하는 것입니다. 발견 된 경우 다음 문자를 테스트하여 문자인지 확인하십시오. 편지 인 경우, 단어의 하위 텍스트를 찾았으며이 찾기를 버리는 것을 알고 있습니다.
// Test online at http://writecodeonline.com/php/
$aWords = array("I", "cat", "sky", "dog");
$aFound = array();
$sSentence = "I have a cat. I don't have cats. I like the sky, but not skyrim.";
foreach ($aWords as $word) {
$pos = strpos($sSentence, $word);
// If found, the position will be greater than or equal to 0
if (!($pos >= 0)) continue;
$nextChar = substr($sSentence , ($pos + strlen($word)), 1);
// If found, ensure it is not a substring
if (ctype_alpha($nextChar)) continue;
$aFound[] = $word;
}
print_r($aFound); // Array ([0] => I [1] => cat [2] => sky)
은 물론 더 나은 솔루션은 이러한 솔루션이 갑자기 근처 효율적으로 패턴을 추구는 것 같이 될 것입니다 당신이, 정규식을 사용할 수없는 이유를 확인하는 것입니다.
사용중인 PHP 버전은 무엇입니까? –