如何在PHP中将不在范围[0x5E10,0x7F35]中的字符替换为'*'? [英] How do I replace characters not in range [0x5E10, 0x7F35] with '*' in PHP?

查看:146
本文介绍了如何在PHP中将不在范围[0x5E10,0x7F35]中的字符替换为'*'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉正则表达式如何处理十六进制,任何人都知道? 解决方案

  $ str =一些旅游景点; 

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

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

重要的是 u -modifier(请参阅此处):
$ b


此修饰符打开
与Perl不兼容的PCRE的额外
功能。 Pattern
字符串被视为UTF-8。这个
修饰符可以从Unix上的PHP 4.1.0
或更高版本和win32上的PHP 4.2.3
上获得。

here 一个简短的描述为什么 \\\￿ 不起作用在PHP中:


Perl和PCRE不支持
\FFFF语法。他们使用\x {FFFF}
代替。您可以省略
中前导零的
大括号之间的十六进制数字。由于\ x本身就是
不是一个有效的正则表达式标记,所以\ x {1234}
永远不会被混淆以匹配\ x 1234
次。它始终与Unicode
代码点U + 1234匹配。 \x {1234} {5678}将
尝试匹配代码点U + 1234完全
5678次。



I'm not familiar with the how regular expressions treat hexadecimal, anyone knows?

解决方案

The following does the trick:

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

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

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

The important thing is the u-modifier (see 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.

And here a short description why \uFFFF is not working in 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中将不在范围[0x5E10,0x7F35]中的字符替换为'*'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆