如何使用多个分隔符分割字符串,后跟或不加空格(混合)?

发布于 2024-11-18 23:20:16 字数 277 浏览 4 评论 0原文

我正在寻找一种行为类似于爆炸但使用多个字符串分隔符的东西,即

+ - (

可以全部都是分隔符。

例如,在“爆炸”以下字符串之后:

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

逆蝶 2024-11-25 23:20:16
$string = 'We are - (the + champions';
$words = preg_split('@[\W]+@', $string)

这样你就得到了 [We, are, the, Champions]

$string = 'We are - (the + champions';
$words = preg_split('/[\+\-\(]/', $string)

这样你就可以保留空格,得到 ['We are', ' ', 'the', 'champions'];有必要进行修剪。

 $string = 'We are 3+4-8 - (the + champions';
 $words = preg_split('/[\+\-] |[\(]/', $string)

有了这个,最后,你得到了['我们是3+4+8','the','冠军']。在这种情况下不需要修剪。

$string = 'We are - (the + champions';
$words = preg_split('@[\W]+@', $string)

With this you obtain [We, are, the, champions]

$string = 'We are - (the + champions';
$words = preg_split('/[\+\-\(]/', $string)

With this you preserve whitespaces obtaining ['We are', ' ', 'the', 'champions']; it would be necessary a trim.

 $string = 'We are 3+4-8 - (the + champions';
 $words = preg_split('/[\+\-] |[\(]/', $string)

With this, finally, you obtain ['We are 3+4+8', 'the', 'champions']. Trim is not necessary in this case.

小忆控 2024-11-25 23:20:16

preg_split() 与字符类。

$chars = '+-(';
$regexp = '/[' . preg_quote($chars, '/') . ']/';
$parts = preg_split($regexp, $string);

忘记补充一点,如果您尝试解析表达式(例如搜索查询),preg_split() 不会削减它,您将需要一个完整的解析器。我想Zend Framework中一定有一个。

Use preg_split() with a character class.

$chars = '+-(';
$regexp = '/[' . preg_quote($chars, '/') . ']/';
$parts = preg_split($regexp, $string);

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.

夜唯美灬不弃 2024-11-25 23:20:16

这将通过 -+( 分割字符串

$result = preg_split(/[ \- ]|[ \+ ]|[(]/im, $string);
$i = 0;
foreach ($result as $match){ 
  $result[$i] = trim($match);
}

This will split your string by either -, +, or (

$result = preg_split(/[ \- ]|[ \+ ]|[(]/im, $string);
$i = 0;
foreach ($result as $match){ 
  $result[$i] = trim($match);
}
人海汹涌 2024-11-25 23:20:16
$string = 'We are - (the + champions';

$split = preg_split('/[\-,\(,\+]/', $string);
$string = 'We are - (the + champions';

$split = preg_split('/[\-,\(,\+]/', $string);
鼻尖触碰 2024-11-25 23:20:16

怎么样:

$str = 'We are 3+4-8 - (the + champions';
$res = preg_split('/\s+[+(-]\s+/', $str);
print_r($res);

输出:

[0] => We are 3+4-8
[1] => (the
[2] => champions

How about:

$str = 'We are 3+4-8 - (the + champions';
$res = preg_split('/\s+[+(-]\s+/', $str);
print_r($res);

output:

[0] => We are 3+4-8
[1] => (the
[2] => champions
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文