This implementation is wrong, did you test it? pg_escape_string() must be before strtr().