How to replace characters not in the range [0x5E10, 0x7F35] with "*" in PHP?

I am not familiar with how regular expressions handle hexadecimal, does anyone know?

+5
source share
1 answer

Below is the trick:

$str = "some മനുഷ്യന്റെ";

echo preg_replace('/[\x{00ff}-\x{ffff}]/u', '*', $str);
// some **********

echo preg_replace('/[^\x{00ff}-\x{ffff}]/u', '*', $str);
// *****മനുഷ്യന്റെ

Important is u-modifier (see here ):

This modifier includes additional PCRE functionality that is incompatible with Perl. String patterns are treated as UTF-8. This Modifier is available with PHP 4.1.0 or higher on Unix and PHP 4.2.3 on win32. The UTF-8 justice template is tested with PHP 4.3.5.

And here is a brief description of why it \uFFFFdoes not work in PHP:

Perl PCRE \ uFFFF. \x {FFFF} . . \x , \x {1234} , \x 1234 . Unicode U + 1234.\x {1234} {5678} U + 1234 5678 .

+14

All Articles