Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed cleanString() and test

Signed-off-by: Darren Hill <dchill42@gmail.com>
  • Loading branch information...
commit 622ead6fc313d09b5b7254d49bf6fb905e47f337 1 parent f6148dc
@dchill42 dchill42 authored
Showing with 14 additions and 6 deletions.
  1. +12 −2 system/core/Utf8.php
  2. +2 −4 tests/Xylophone/core/Utf8Test.php
View
14 system/core/Utf8.php
@@ -90,8 +90,18 @@ public function __construct()
*/
public function cleanString($str)
{
- // Convert string if enabled and not ASCII
- return (!$this->iconv_enabled || $this->isAscii($str)) ? $str : @iconv('UTF-8', 'UTF-8//IGNORE', $str);
+ // Return string if disabled or ASCII
+ if (!$this->iconv_enabled || $this->isAscii($str)) {
+ return $str;
+ }
+
+ // Try "converting" the string, ignoring bad characters.
+ // This call behaves differently in PHP 5.3 and > 5.3, perhaps
+ // also depending on glibc version of the server. In short, later
+ // versions return FALSE on a non-UTF8 string, so all we can
+ // consistently do is scrap the whole string if it fails.
+ $ret = @iconv('UTF-8', 'UTF-8//IGNORE', $str);
+ return ($ret === $str) ? $str : '';
}
/**
View
6 tests/Xylophone/core/Utf8Test.php
@@ -104,10 +104,8 @@ public function testCleanString()
$utf8->expects($this->once())->method('isAscii')->will($this->returnValue(false));
$utf8->iconv_enabled = true;
- // Set up args and call cleanString()
- $clean = 'hi';
- $str = $clean."\x80\x81";
- $this->assertEquals($clean, $utf8->cleanString($str));
+ // Call cleanString()
+ $this->assertEmpty($utf8->cleanString("\x80\x81"));
}
/**
Please sign in to comment.
Something went wrong with that request. Please try again.