php equivalent of the perl script