bug #16359 Use mb_detect_encoding with $strict = true (nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

Use mb_detect_encoding with $strict = true

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | -
| License       | MIT
| Doc PR        | -

Otherwise, UTF-8 can be returned for non-UTF8 strings...
See e.g. https://3v4l.org/oMMnX

Commits
-------

e6c89f1 Use mb_detect_encoding with $strict = true
This commit is contained in:
Fabien Potencier 2015-10-27 19:13:23 -07:00
commit c7e772c8d8
5 changed files with 6 additions and 6 deletions

View File

@ -160,7 +160,7 @@ class DbalLoggerTest extends \PHPUnit_Framework_TestCase
$dbalLogger $dbalLogger
->expects($this->once()) ->expects($this->once())
->method('log') ->method('log')
->with('SQL', array('short' => $shortString, 'long' => mb_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, mb_detect_encoding($longString)).' [...]')) ->with('SQL', array('short' => $shortString, 'long' => mb_substr($longString, 0, DbalLogger::MAX_STRING_LENGTH - 6, 'UTF-8').' [...]'))
; ;
$dbalLogger->startQuery('SQL', array( $dbalLogger->startQuery('SQL', array(

View File

@ -1089,7 +1089,7 @@ class Application
return strlen($string); return strlen($string);
} }
if (false === $encoding = mb_detect_encoding($string)) { if (false === $encoding = mb_detect_encoding($string, null, true)) {
return strlen($string); return strlen($string);
} }
@ -1106,7 +1106,7 @@ class Application
return str_split($string, $width); return str_split($string, $width);
} }
if (false === $encoding = mb_detect_encoding($string)) { if (false === $encoding = mb_detect_encoding($string, null, true)) {
return str_split($string, $width); return str_split($string, $width);
} }

View File

@ -53,7 +53,7 @@ abstract class Helper implements HelperInterface
return strlen($string); return strlen($string);
} }
if (false === $encoding = mb_detect_encoding($string)) { if (false === $encoding = mb_detect_encoding($string, null, true)) {
return strlen($string); return strlen($string);
} }

View File

@ -386,7 +386,7 @@ class TableHelper extends Helper
$width = $this->getColumnWidth($column); $width = $this->getColumnWidth($column);
// str_pad won't work properly with multi-byte strings, we need to fix the padding // str_pad won't work properly with multi-byte strings, we need to fix the padding
if (function_exists('mb_strwidth') && false !== $encoding = mb_detect_encoding($cell)) { if (function_exists('mb_strwidth') && false !== $encoding = mb_detect_encoding($cell, null, true)) {
$width += strlen($cell) - mb_strwidth($cell, $encoding); $width += strlen($cell) - mb_strwidth($cell, $encoding);
} }

View File

@ -132,7 +132,7 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like'); throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
} }
if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value)) { if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value, null, true)) {
$length = mb_strlen($value, $encoding); $length = mb_strlen($value, $encoding);
$remainder = mb_substr($value, $position, $length, $encoding); $remainder = mb_substr($value, $position, $length, $encoding);
} else { } else {