php - 如何在 PHP 中用 '*' 替换不在 [0x5E10, 0x7F35] 范围内的字符?

标签 php regex hex

我不熟悉正则表达式如何处理十六进制,有人知道吗?

最佳答案

下面的技巧:

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

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

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

重要的是 u 修饰符(参见 here ):

This modifier turns on additional functionality of PCRE that is incompatible with Perl. Pattern strings are treated as UTF-8. This modifier is available from PHP 4.1.0 or greater on Unix and from PHP 4.2.3 on win32. UTF-8 validity of the pattern is checked since PHP 4.3.5.

here简短说明为什么 \uFFFF 在 PHP 中不起作用:

Perl and PCRE do not support the \uFFFF syntax. They use \x{FFFF} instead. You can omit leading zeros in the hexadecimal number between the curly braces. Since \x by itself is not a valid regex token, \x{1234} can never be confused to match \x 1234 times. It always matches the Unicode code point U+1234. \x{1234}{5678} will try to match code point U+1234 exactly 5678 times.

关于php - 如何在 PHP 中用 '*' 替换不在 [0x5E10, 0x7F35] 范围内的字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2728070/

相关文章:

php - mysqli_insert_id 不断返回 0

javascript - 以特定方式分割字符串 JavaScript

ruby - 使用正则表达式选择引号中的内容

javascript - 如何确定两种 HEX 颜色之间的差异?

python - 带有十六进制输入的 Python 中的 CRC32 校验和

javascript - 从数据库加载 iframe

php - netshel paypal 中的 getCheckout 函数出错

php - 如何通过第三方 API 安全维护用户身份验证?

javascript - 如何根据字符串的长度用 "*"(astrik) 字母替换字符串

c++ - GetAsyncKeyState 的返回值似乎与描述不符