将字符串分成几部分,返回所有字符
我想根据以下规则打破字符串:
- 所有连续的字母数字字符加上点(
.
)必须被视为一部分 - 所有其他连续字符必须被视为
- 连续组合的 一部分
1
和2
必须被视为不同的部分, - 不得返回空格
例如这个字符串:
Method(hierarchy.of.properties) = ?
应该返回这个数组:
Array
(
[0] => Method
[1] => (
[2] => hierarchy.of.properties
[3] => )
[4] => =
[5] => ?
)
我使用 preg_split()< 失败了/代码>,如AFAIK 它不能将模式视为要返回的元素。
有什么简单的方法来做到这一点吗?
I want to break a string according to the following rules:
- all consecutive alpha-numeric chars, plus the dot (
.
) must be treated as one part - all other consecutive chars must be treated as one part
- consecutive combinations of
1
and2
must be treated as different parts - no whitespace must be returned
For example this string:
Method(hierarchy.of.properties) = ?
Should return this array:
Array
(
[0] => Method
[1] => (
[2] => hierarchy.of.properties
[3] => )
[4] => =
[5] => ?
)
I was unsuccessful with preg_split()
, as AFAIK it cannot treat the pattern as an element to be returned.
Any idea for a simple way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能应该使用 preg_match_all 而不是 preg_split。
输出:
You probably should use preg_match_all over preg_split.
Output:
这应该做你想要的:
我使用了一个循环来删除所有空白匹配,因为据我所知 preg_split() 中没有适合你的功能。
This should do what you want:
I used a loop to remove all whitespace matches, since as far as I know there isn't a feature in preg_split() to that for you.