The function you are looking for is htmlentities
http://php.net/manual/en/function.htmlentities.php
You'll need to know which encoding the input data uses (obviously). The default is UTF8.
$encoded = htmlentities( $input, ENT_COMPAT | ENT_HTML401, 'UTF-8', false );
Note that I've set the final parameter false
(default is true
). That is so that users can type, for example, &
and it will convert to &
. But maybe you want the default behaviour (&
-> &
)
The encoding which is used when sending POST data can be set in the form
tag:
<form method="post" action="myscript.php" accept-charset="utf-8">
But see here for a discussion: Is there any benefit to adding accept-charset="UTF-8" to HTML forms, if the page is already in UTF-8?