php:charset_converter
This is a very specific charset converter, but you should be able to rewrite the code to fit your own needs. It's meant to convert an XML file from UTF-8 to ISO-8859-1, nothing else (well maybe the other way around too, I made support for that anyway).
- NOTE: If you need anything else but XML, you need to change “Content-Type”. It should be very easy to automate, I was just too lazy (maybe you can use $_FILES['convertme']['type'] - Please send me the changes if you do fix it).
- NOTE: In the uploaded file I also replace 'encoding=“UTF-8”' with 'encoding=“ISO-8859-1”' (or whatever charset they choose), so you probably need to comment that out.
<?php // Which charsets do we support? $charsets[] = "ISO-8859-1"; $charsets[] = "UTF-8"; if (!isset($_POST['charsetto'])) { ?> <html> <title>Charset converter</title> <body> <center><h3>Charset converter</h3></center> <form action="?" method="post" enctype="multipart/form-data"> Convert charset from: <select name="charsetfrom"> <?php foreach (array_reverse($charsets) as $charset) { echo "<option value=\"".$charset."\">".$charset."</option>"; } ?> </select><br> Convert charset to: <select name="charsetto"> <?php foreach ($charsets as $charset) { echo "<option value=\"".$charset."\">".$charset."</option>"; } ?> </select><br> <input type="file" name="convertme" id="convertme"><br> <input type="submit" value="Convert"> </form> </body> </html> <?php } else { $readcontent = file_get_contents($_FILES['convertme']['tmp_name']); $convertedcontent = iconv($_POST['charsetfrom'], $_POST['charsetto'], $readcontent); // Replace encoding header $convertedcontent = str_replace("encoding=\"".$_POST['charsetfrom']."\"", "encoding=\"".$_POST['charsetto']."\"", $convertedcontent); header("Content-Description: File Transfer"); // header("Content-Type: application/octet-stream"); header("Content-Type: text/xml"); header("Content-Disposition: attachment; filename=".basename($FILES['convertme']['name'])); header("Content-Transfer-Encoding: binary"); header("Expires: 0"); header("Cache-Control: must-revalidate"); header("Pragma: public"); header("Content-Length: ".$FILES['convertme']['size']); ob_clean(); flush(); echo $convertedcontent; } ?>
php/charset_converter.txt · Last modified: 2015/08/15 22:56 by 127.0.0.1