eval.in

Paste #54506

PHP — PHP 5.4.6, pasted 6 months ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/**
 * @link http://stackoverflow.com/q/19316127/367456
 */

$s = "THE 2.5±0.1; 0.5±0.2 UTF-8 NÔ-BRÉ\xC2\xADAK\xC2\xA0SPACE"; // a complex UTF8
$result = preg_match_all('/[-\'\p{L}\xC2\xAD]+/u',$s,$m);

if ($result === FALSE) {
    switch (preg_last_error()) {
        case PREG_BAD_UTF8_ERROR:
            throw new InvalidArgumentException(
                'UTF-8 encoded binary string expected.'
            );
        default:
            throw new RuntimeException('preg error occured.');
            
    }
}
var_dump($m);            // ok...
print_r( str_word_count($s, 1, "ÔÉ") ); // craches with "ÔÉ\xC2\xA0S"!

Program Output

array(1) {
  [0]=>
  array(4) {
    [0]=>
    string(3) "THE"
    [1]=>
    string(4) "UTF-"
    [2]=>
    string(12) "NÔ-BRÉ­AK"
    [3]=>
    string(5) "SPACE"
  }
}
Array
(
    [0] => THE
    [1] => UTF-
    [2] => NÔ-BRÉ
    [3] => AK
    [4] => SPACE
)

OK (0.008 sec real, 0.009 sec wall, 13 MB, 116 syscalls)

Fork