替换单词IF的第一个和最后一个匹配项

With this peace of code i replace the first and the last occurence of a word in string.

    $text = 'A United States aircraft fly in United States in the United States area.';
    $find = 'United States';
    $replace = ' U.S ';

    $firsto =  preg_replace("/$find/", "$replace", $text, 1); // stolen in stackoverflow 

    $lasto = preg_replace(strrev("/$find/"),strrev($replace),strrev($firsto),1); // stolen in stackoverflow 

    $final = strrev($lasto);

    echo "<br><br> $final"; 

In this example there are 3 occurrences of the word United States and the code below replace the first and the last occurrence.

   And Output: A U.S aircraft fly in United States in the U.S area.

my problem is that if the occurrence of word = 2 like this:

         $text = 'A United States aircraft fly in United States';

Using a modified version of the code above i want to replace only the first occurrence if there are only 2 occurrences and output something like this.

     Output: A U.S aircraft fly in United States.

else if occurrence > 2 use the code below. thanks

$firsto =  preg_replace("/$find/", "$replace", $text, 1); // outputs '123def abcdef abcdef'

$final = $firsto;
$m;
if(preg_match_all("/($find)/",$firsto, $m) && count($m[1]) > 1){
    $lasto = preg_replace(strrev("/$find/"),strrev($replace),strrev($firsto),1);
    $final = strrev($lasto);
}
if (substr_count($text,$find) == 2){
    $firsto =  preg_replace("/$find/", "$replace", $text, 1);
}
else{
    ... use existing
}