如何使用多个分隔符分割字符串,后跟或不加空格(混合)?
我正在寻找一种行为类似于爆炸但使用多个字符串分隔符的东西,即
+ - (
可以全部都是分隔符。
例如,在“爆炸”以下字符串之后:
$string = 'We are 3+4-8 - (the + champions'
我应该将其获取为 $string[0]:
['We are 3+4-8']
是否有任何函数可以这样做?
I'm looking for something that acts just like explode but using more than one string separator, i.e.
+ - (
could all be separators.
For example, after "exploding" the following string:
$string = 'We are 3+4-8 - (the + champions'
I should get this as $string[0]:
['We are 3+4-8']
Is there any function that acts that way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这样你就得到了 [We, are, the, Champions]
这样你就可以保留空格,得到 ['We are', ' ', 'the', 'champions'];有必要进行修剪。
有了这个,最后,你得到了['我们是3+4+8','the','冠军']。在这种情况下不需要修剪。
With this you obtain [We, are, the, champions]
With this you preserve whitespaces obtaining ['We are', ' ', 'the', 'champions']; it would be necessary a trim.
With this, finally, you obtain ['We are 3+4+8', 'the', 'champions']. Trim is not necessary in this case.
将
preg_split()
与字符类。忘记补充一点,如果您尝试解析表达式(例如搜索查询),
preg_split()
不会削减它,您将需要一个完整的解析器。我想Zend Framework中一定有一个。Use
preg_split()
with a character class.Forgot to add that if you're trying to parse expressions such as search queries,
preg_split()
won't cut it and you'll need a full fledged parser. I think there must be one in the Zend Framework.这将通过
-
、+
或(
分割字符串This will split your string by either
-
,+
, or(
怎么样:
输出:
How about:
output: