Bug #77766 [NEW]: mb_detect_encoding() returns false for strings with any chr(128..193|245..255)

From: Date: Mon, 18 Mar 2019 14:55:58 +0000
Subject: Bug #77766 [NEW]: mb_detect_encoding() returns false for strings with any chr(128..193|245..255)
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-220049@lists.php.net to get a copy of this message
From: ca at lsp dot net Operating system: Linux + Windows PHP version: 7.2.16 Package: mbstring related Bug Type: Bug Bug description:mb_detect_encoding() returns false for strings with any chr(128..193|245..255) Description: ------------ mb_detect_encoding() fails if $str contains an ASCII character between 128 and 193 or between 245 and 255. I noticed this after getting exceptions when importing emails (into MySQL 8 using PDO with utf8mb4). The email had raw non-breaking spaces (i.e. &nbsp; in decoded form) and, apparently, PDO couldn't properly encode them: $sql = "INSERT INTO emails (..., body, ...) VALUES (..., '...\xA0...', ...)"; $pdo->query($sql); SQLSTATE[HY000]: General error: 1366 Incorrect string value: '...\xA0...' for column ... Workaround: Encode the value or query conditionally (if mb_detect_encoding() === false) using utf8_encode. P.S.: Quoting the value using PDO::quote and/or using a prepared statement yielded the same result. --- From manual page: https://php.net/function.mb-detect-encoding --- Test script: --------------- <?php var_dump( mb_detect_encoding("INSERT INTO emails (body) VALUES ('Hello\xA0world!')") ); for ($i = 0; $i <= 255; $i++) { $chr = chr($i); if (false === mb_detect_encoding($chr)) { printf("%d ", $i); } } Expected result: ---------------- string(5) "UTF-8" Actual result: -------------- bool(false) 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 245 246 247 248 249 250 251 252 253 254 255 -- Edit bug report at https://bugs.php.net/bug.php?id=77766&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77766&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77766&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77766&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=77766&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=77766&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=77766&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=77766&r=needscript Try newer version: https://bugs.php.net/fix.php?id=77766&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=77766&r=support Expected behavior: https://bugs.php.net/fix.php?id=77766&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=77766&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=77766&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=77766&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77766&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=77766&r=dst IIS Stability: https://bugs.php.net/fix.php?id=77766&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=77766&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=77766&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=77766&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=77766&r=mysqlcfg

« previous php.bugs (#220049) next »