1

I have the following phpunit assert:

$this->assertRegExp('/^\d.\d{3}$/i', $this->object->transformNumber('3333', 'de_AT'));
// $number = '3333' and $lang = 'de_AT'

and the relevant part of transformNumber:

$formatter = new \NumberFormatter($lang, \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, 2);

return $formatter->format($number);

Here intl acts different I've learned that because of the different Intl Versions different outputs can result.

Now my question is why do I get this and the unit test fails:

Failed asserting that '3 333' matches PCRE pattern "/^\d.\d{3}$/i".

As it clear matches the result: regex 101

Info: Runtime: PHP 7.0.20 with Xdebug 2.5.4

EDIT:

After I rewrote the test to:

    $result = $this->object->transformNumber('3333', 'de_AT');
    printf($result);
    echo "\n";
    foreach (str_split($result) as $char) {
        printf('char: %s => ord: %s', $char, ord($char));
        echo "\n";
    }

I got the follwoing output:

3 333
char: 3 => ord: 51
char: � => ord: 194
char: � => ord: 160
char: 3 => ord: 51
char: 3 => ord: 51
char: 3 => ord: 51

That means that on some machines with different setup (php version/ intl version and so on) I get an extra character (actually 2 Ascii chars instead of space) in the number. Is this an Intl problem?

And I've adjusted the regex to:

$this->assertRegExp('/^\d.(.)?\d{3}$/i', $result);

Now a new question is rising: Why do I get this encoding problem only on some environments?

Edwin
  • 2,146
  • 20
  • 26
  • Your failure message has the number in quotes. I wouldn't expect quotes around a number so is it being parsed as a string which includes those quotes? If so your regex will fail because it contains the start of string and end if string anchors so is expecting a 3 at both ends. – miknik Nov 15 '17 at 17:18
  • that's not true. First of all I've tried also with `3333` without the quotes and I get the same error. Second, https://phpunit.de/manual/current/en/appendixes.assertions.html#appendixes.assertions.assertRegExp expects the second param as a string and will apply the pattern on the string. – Edwin Nov 16 '17 at 08:01
  • 194 160 is decimal code for U+00A0, a non break space in UTF-8. Sounds like some of your environments are using different character encoding. – miknik Nov 16 '17 at 16:42

0 Answers0