Skip to content

Commit

Permalink
[+]: add some more tests v2
Browse files Browse the repository at this point in the history
  • Loading branch information
voku committed Oct 24, 2016
1 parent ae6d423 commit 9dba3d1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -823,9 +823,9 @@ Remove invisible characters from a string.
UTF8::remove_invisible_characters("κόσ\0με"); // 'κόσμε'
```

##### replace_diamond_question_mark(string $str, string $unknown = '?') : string
##### replace_diamond_question_mark(string $str, string $replacementChar = '', bool $processInvalidUtf8 = true) : string

Replace the diamond question mark (�) with the replacement.
Replace the diamond question mark (�) and invalid-UTF8 chars with the replacement.

```php
UTF8::replace_diamond_question_mark('中文空白�', ''); // '中文空白'
Expand Down
39 changes: 21 additions & 18 deletions src/voku/helper/UTF8.php
Expand Up @@ -3786,44 +3786,47 @@ public static function remove_invisible_characters($str, $url_encoded = true, $r
}

/**
* Replace the diamond question mark (�) with the replacement.
* Replace the diamond question mark (�) and invalid-UTF8 chars with the replacement.
*
* @param string $str <p>The input string</p>
* @param string $unknown <p>The replacement character.</p>
* @param string $str <p>The input string</p>
* @param string $replacementChar <p>The replacement character.</p>
* @param bool $processInvalidUtf8 <p>Convert invalid UTF-8 chars </p>
*
* @return string
*/
public static function replace_diamond_question_mark($str, $unknown = '?')
public static function replace_diamond_question_mark($str, $replacementChar = '', $processInvalidUtf8 = true)
{
$str = (string)$str;

if (!isset($str[0])) {
return '';
}

$unknownHelper = $unknown;
if ($unknown === '') {
$unknownHelper = 'none';
}
if ($processInvalidUtf8 === true) {
$replacementCharHelper = $replacementChar;
if ($replacementChar === '') {
$replacementCharHelper = 'none';
}

if (self::$support['mbstring'] === false) {
trigger_error('UTF8::replace_diamond_question_mark() without mbstring cannot handle all chars correctly', E_USER_WARNING);
}
if (self::$support['mbstring'] === false) {
trigger_error('UTF8::replace_diamond_question_mark() without mbstring cannot handle all chars correctly', E_USER_WARNING);
}

$save = \mb_substitute_character();
\mb_substitute_character($unknownHelper);
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$str = \mb_convert_encoding($str, 'UTF-8', 'UTF-8');
\mb_substitute_character($save);
$save = \mb_substitute_character();
\mb_substitute_character($replacementCharHelper);
/** @noinspection CallableParameterUseCaseInTypeContextInspection */
$str = \mb_convert_encoding($str, 'UTF-8', 'UTF-8');
\mb_substitute_character($save);
}

return str_replace(
array(
"\xEF\xBF\xBD",
'�',
),
array(
$unknown,
$unknown,
$replacementChar,
$replacementChar,
),
$str
);
Expand Down
8 changes: 8 additions & 0 deletions tests/Utf8GlobalTest.php
Expand Up @@ -2091,6 +2091,14 @@ public function testReplaceDiamondQuestionMark()
self::assertSame($after, UTF8::replace_diamond_question_mark($before, ''), 'tested: ' . $before . ' | counter: ' . $counter);
++$counter;
}

// ---

self::assertSame('Iñtërnâtiônàlizætiøn??Iñtërnâtiônàlizætiøn', UTF8::replace_diamond_question_mark("Iñtërnâtiônàlizætiøn\xa0\xa1Iñtërnâtiônàlizætiøn", '?', true));

// ---

self::assertSame("Iñtërnâtiônàlizætiøn\xa0\xa1Iñtërnâtiônàlizætiøn", UTF8::replace_diamond_question_mark("Iñtërnâtiônàlizætiøn\xa0\xa1Iñtërnâtiônàlizætiøn", '?', false));
}

public function testRtrim()
Expand Down

0 comments on commit 9dba3d1

Please sign in to comment.