Well...
I just created a php file with you example above and installed it into my host.
A did run the php file and it doesn't execute the code inside the if.
Are you sure that your example returns true?
Almost none of my codes posted here are tested and are just from the top of my head. They should be used as a guideline if nothing else.
Which part of the If are you talking about? The one in the foreach loop? Could you do a var_dump of the IP given by the explode+str_replace method and some of the TOR ips the foreach loop returns?