What about the case where $str1 is never found? The solutions so far presented seem to ignore that case and the results appear to be indistinguishable from first finding $str1 and then $str2.
You must always remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.