http://it2.php.net/manual/en/function.urlencode.php

http://it2.php.net/manual/en/function.htmlentities.php

ci sono anche gli esempi su come costruire un url abbastanza corretto