3v4l.org

run code in 300+ PHP versions simultaneously
<?php $txt = 'dog blah crocodile blah dog blah cat blah crocodile blah dog blah crocodile blah dog blah cat blah dog blah cat blah crocodile blah dog blah crocodile blah mice blah cat blah crocodile'; echo preg_replace('/(?:dog|mice)(?:(?!dog|mice|cat).)* \Kcrocodile(?= (?:(?!dog|mice|cat).)* (?:dog|mice))/', 'replacement', $txt);
based on mJFHB
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.24, 7.4.0 - 7.4.12, 8.0.0
dog blah replacement blah dog blah cat blah crocodile blah dog blah replacement blah dog blah cat blah dog blah cat blah crocodile blah dog blah replacement blah mice blah cat blah crocodile