Here's something to try - $sql is the candidate query:
if(@pg_exec($connection, "EXPLAIN $sql"))
{...valid query...}
else
{...invalid query - error accessible from pg_errormessage()
}
@ suppresses the error message, EXPLAIN prevents the query from actually going ahead prematurely.
That's what I'd try first. It won't work if EXPLAINed queries return false for some reason.