【问题标题】:/u modifier for special characters does not work in regex特殊字符的 /u 修饰符在正则表达式中不起作用
【发布时间】:2017-04-21 08:52:35
【问题描述】:

我正在使用 ´ 等特殊字符进行编码,对于它们,我需要 /u 修饰符,但它仍然无法显示它们。我的代码:

$input = array("⋃","⋃","a","⋃","h");

$input = implode($input);

$input = Normalizer::normalize($input); // unite binary code

$pattern = '/⋃{2}/u';

$replacement = '$0|';

$output = str_split(preg_replace($pattern,$replacement,$input));

【问题讨论】:

  • 您是否使用 UTF8 编码保存页面?
  • str_split 不是多字节安全的。 str_split() will split into bytes, rather than characters when dealing with a multi-byte encoded string. 3v4l.org/ZvKSO preg_replace 按我的预期工作。
  • 这里的目标是什么?在 double Us 后面加|,然后拆分成 Unicode 点?
  • @chris85 那么str_split() 有没有不分割成字节的替代方案?
  • @WiktorStribiżew 的目标是,在特定模式之后,我想将一个项目插入到数组中......直到现在这段代码是我需要的最可靠的代码。

标签: php arrays regex binary


【解决方案1】:

由于您需要将 Unicode 字符串标记为 Unicode 字符,我建议在这里使用preg_ 函数。

$input = array("⋃","⋃","a","⋃","h");
$impl = implode($input);
$impl = preg_replace('/⋃{2}/u','$0|',$impl);
preg_match_all('~\X~u', $impl, $tokens);
print_r($tokens);

PHP demo

首先implode,然后preg_replace 在双 之后添加|,然后使用preg_match_all\X 模式匹配任何Unicode 字形。

【讨论】:

  • 累积。到pcre man page\XUnicode 扩展字形簇。在该页面上查找扩展字素簇
  • 之后是什么值?因为作为一个数组,您既不能使用$tokens 也不能使用$impl
  • 为什么我用$tokens操作总是报错1?
  • @ChrisWinterbottom 您将需要提供更多信息(什么错误,您如何使用它等),也可能应该是一个新问题。作为一个数组为我工作,eval.in/692147.
  • 我认为与当前问题无关。
猜你喜欢
  • 2012-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 2018-06-13
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多