Detect encoding and use that in if statement

Try to detect encoding type and check on it not being ASCII in if
statement too
This commit is contained in:
Matt Pass
2013-05-22 17:21:17 +01:00
parent e1d1891de8
commit 1fcc2d996c

View File

@@ -53,6 +53,8 @@ function numClean($var) {
// returns a UTF8 based string with any UFT8 BOM removed
function toUTF8noBOM($string,$message) {
// Attempt to detect encoding
$encType = mb_detect_encoding($string);
// Get rid of any UTF-8 BOM
$string = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$string);
// Test for any bad characters
@@ -60,7 +62,7 @@ function toUTF8noBOM($string,$message) {
$teststringBroken = utf8_decode($teststring);
$teststringConverted = iconv("UTF-8", "UTF-8//IGNORE", $teststringBroken);
// If we have a matching length, UTF8 encode it
if (strlen($teststringConverted) == strlen($teststringBroken)) {
if ($encType != "ASCII" && strlen($teststringConverted) == strlen($teststringBroken)) {
$string = utf8_encode($string);
if ($message) {
echo "<script>top.ICEcoder.message('Your document doesn\'t appear to be in UTF-8 encoding so has been converted.');</script>";