You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP如何从指定子串P:后的字符串中提取所有数字并忽略U+200B

实现方案

你原来的正则没有覆盖+、字母、零宽空格等多种分隔符场景,也没有限定仅提取P:之后的数字,所以无法得到正确结果,可按以下逻辑实现:

  1. 先移除字符串中的所有U+200B零宽空格字符
  2. 匹配P:之后到末尾[2]之前的所有连续数字序列即可

示例代码(PHP)

// 原始字符串
$rawStr = "H: 290​‐​314 P: 280​‐​301+330​​​​U+200B+331​string‐​305+351+338​‐​308+310 [2]";

// 移除所有U+200B零宽空格
$processedStr = str_replace("\u{200B}", "", $rawStr);

// 匹配P:之后到[之前的所有数字
preg_match_all('/(?:P:\s|\G(?!^))[^[\d]*\K\d+/', $processedStr, $matches);

// 转换为整型数组,得到最终结果
$result = array_map('intval', $matches[0]);
var_dump($result);

输出结果

array(9) {
  [0]=> int(280)
  [1]=> int(301)
  [2]=> int(330)
  [3]=> int(331)
  [4]=> int(305)
  [5]=> int(351)
  [6]=> int(338)
  [7]=> int(308)
  [8]=> int(310)
}

内容的提问来源于stack exchange,提问作者Alexander Weixelbaumer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 23:54:04