[英]regex - match empty space between any of characters
In PHP it is a common practice to treat strings as immutable.在 PHP 中,将字符串视为不可变是一种常见做法。 Sometimes there's a need to modify a string "in-place".有时需要“就地”修改字符串。
We go with the additional array creation approach.我们采用额外的数组创建方法。
This array should contain every single letter from the source string.此数组应包含源字符串中的每个字母。
There's a function for that in PHP (str_split). PHP中有一个函数(str_split)。 One issue, it doesn't handle multibyte encodings well enough.一个问题,它不能很好地处理多字节编码。
There's also a mb_split function which takes a regex as an input parameter for separator sequence.还有一个 mb_split 函数,它将正则表达式作为分隔符序列的输入参数。 So所以
mb_split('.', '123')
returns ['', '', '', ''].返回 ['', '', '', '']。
BUT:但:
mb_split('', '123')
returns ['123'].返回 ['123']。
So I believe there is a counterpart regex which matches empty space between any variation of multi-byte character sequence.所以我相信有一个对应的正则表达式匹配多字节字符序列的任何变体之间的空格。
So for '123' it should match所以对于'123'它应该匹配
'1~2', '2~3'
where ~ is an actual match.其中 ~ 是实际匹配。 That is just like \\b but for anything.这就像 \\b 但对于任何事情。
Is there a regex hack to do so?是否有正则表达式黑客这样做?
Use用
preg_match_all('~\X~u', $s, $arr)
The $arr[0]
will contain all the characters. $arr[0]
将包含所有字符。 The \\X
pattern matches any Unicode grapheme . \\X
模式匹配任何 Unicode 字素。 The /u
modifier is necessary to make the regex engine treat the input string as a Unicode string and make the pattern Unicode aware. /u
修饰符对于使正则表达式引擎将输入字符串视为 Unicode 字符串并使模式识别 Unicode 是必需的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.