基本上我想带一个字符串:
"D1F21a"
并将其转换为如下数组:
Array
(
[0] => D
[1] => 1
[2] => F
[3] => 21
[4] => a
)
我已经尝试过this solution但似乎无法成功地让它在PHP中运行而不会出现未知修饰符错误。
答案 0 :(得分:2)
您可以尝试在每个数字处拆分字符串,捕获过程中的数字:
$arr = preg_split('/(\d+)/', 'D1F21a', -1, PREG_SPLIT_DELIM_CAPTURE);
会导致:
array(5) {
[0] => string(1) "D"
[1] => string(1) "1"
[2] => string(1) "F"
[3] => string(2) "21"
[4] => string(1) "a"
}
答案 1 :(得分:0)
\K
动词告诉引擎删除与要返回的匹配项相匹配的内容。拆分字符串。
\d+\K|\D+\K
从索引1获取匹配的组
示例代码:
$re = "/(\\d+\\K|\\D+\\K)/";
$str = "D1F21a";
preg_match_all($re, $str, $matches);
您可以在非数字和反之后用数字Lookahead and Lookbehind将其拆分。
(?<=\D)(?=\d)|(?<=\d)(?=\D)
说明:
\D Non-Digit [^0-9]
\d any digit [0-9]
答案 2 :(得分:0)
您链接的正则表达式也适用:
$regexp = '/(?<=\p{L})(?=\p{N})|(?<=\p{N})(?=\p{L})/';
$data = "D1F21a";
$matches = preg_split($regexp, $data);
var_dump($matches);