Rafał Kokosiński Rafał Kokosiński - 1 year ago 53
HTML Question

Change UTF-8 charset to ISO/IEC 8859-2:1999 in href

I have a very simple form, consisting of two files:


<form action="test.php" method="post">

<input type='text' name = 'name' value="ł ą ź ć ę ó" >

<input id='submit' type='submit' name = 'submit' value="SEND">



$name = $_POST['name'];

echo "<a href='http://myaddress/$name/'>NAME</a>"

In the form field 'name' fill Polish characters (ł ą ź ć ę ó)

When you click SEND script generates me a link of the form:

"<a href='http://myaddress/l a ź ć e ó/'>NAME</a>"

A link should generate in the form of:

"<a href='http://myaddress/%B3+%B1+%BC+%E6+%EA+%F3/'>NAME</a>"

Answer Source

The %xx encoded form is percent-encoding or "URL encoding". You can produce it using the rawurlencode function. That function takes the raw bytes of your string and turns them into percent encoded groups. The exact output therefore depends on what bytes exactly you feed in, meaning what encoding your text is in.

If you need "ź" to encode to %BC, then it must be encoded in ISO-8859-2 (as you say). There are two ways to get that:

  1. Ensure your form is submitted as ISO-8859-2 by adding an appropriate attribute:

    <form accept-charset="iso-8859-2">
  2. If you don't submit in the encoding you need, then instead convert it on the server:

    $name = iconv('UTF-8', 'ISO-8859-2', $_POST['name']);

Then rawurlencode($name) should result in %B3+%B1+%BC+%E6+%EA+%F3.