分成几个字符并将它们包含在各个部分中

发布于 2024-11-03 23:39:13 字数 809 浏览 1 评论 0原文

我想将一个字符串拆分为几个字符(+~>@,但我希望这些字符成为返回部分的一部分。

我尝试过:

$parts = preg_split('/\+|>|~|@/', $input, PREG_SPLIT_DELIM_CAPTURE);

结果只有 2 个部分,而应该有 5 个部分,并且 split-char 不是部分 [1] 的一部分。

我也尝试过:

$parts = preg_split('/\+|>|~|@/', $input, PREG_SPLIT_OFFSET_CAPTURE);

结果是 1 个部分。太少(4而不是5)和最后一部分包含 split-char。

如果 preg_split 中没有标志,结果几乎是完美的(尽可能多的部分),但所有 split-char 都消失了

示例:

$input = 'oele>boele@4 + key:type:id + *~the end'; // spaces should be ignored
$output /* should be: */
        array( 'oele', '>boele', ' @4 ', '+ key:type:id ', '+ *', '~the end' );

是否有 spl 函数或标志可以执行此操作,或者我必须自己制作一个 =(

I want to split a string on several chars (being +, ~, > and @, but I want those chars to be part of the returned parts.

I tried:

$parts = preg_split('/\+|>|~|@/', $input, PREG_SPLIT_DELIM_CAPTURE);

The result is only 2 parts where there should be 5 and the split-char isn't part of part [1].

I also tried:

$parts = preg_split('/\+|>|~|@/', $input, PREG_SPLIT_OFFSET_CAPTURE);

The result is then 1 part too few (4 instead of 5) and the last part contains a split-char.

Without flags in preg_split, the result is almost perfect (as many parts as there should be) but all the split-chars are gone.

Example:

$input = 'oele>boele@4 + key:type:id + *~the end'; // spaces should be ignored
$output /* should be: */
        array( 'oele', '>boele', ' @4 ', '+ key:type:id ', '+ *', '~the end' );

Is there a spl function or flag to do this or do I have to make one myself =(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

满身野味 2024-11-10 23:39:13
$parts = preg_split('/(?=[+>~@])/', $input);

查看

因为您希望将分隔符作为下一个拆分的一部分片段,您的分割点就在分隔符之前,这可以使用正向前视轻松完成。

(?=     : Start of positive lookahead
 [+>~@] : character class to match any of your delimiters.
)       : End of look ahead assertion.

实际上,您要求 preg_split 在分隔符之前的点分割输入字符串。

$parts = preg_split('/(?=[+>~@])/', $input);

See it

Since you want to have the delimiters to be part of the next split piece, your split point is right before the delimiter and this can be easily done using positive look ahead.

(?=     : Start of positive lookahead
 [+>~@] : character class to match any of your delimiters.
)       : End of look ahead assertion.

Effectively you are asking preg_split to split the input string at points just before delimiters.

叫嚣ゝ 2024-11-10 23:39:13

您缺少 limit 参数的分配,这就是为什么它返回的结果少于您的预期,请尝试:

$parts = preg_split('/\+|>|~|@/', $input, -1, PREG_SPLIT_OFFSET_CAPTURE);

You're missing an assignment for the limit parameter which is why it's returning less than you expected, try:

$parts = preg_split('/\+|>|~|@/', $input, -1, PREG_SPLIT_OFFSET_CAPTURE);
骑趴 2024-11-10 23:39:13

好吧,我过去也遇到过同样的问题。您必须用方括号将正则表达式括起来,然后希望它可以工作

$parts = preg_split('/(\+|>|~|@)/', $input, PREG_SPLIT_OFFSET_CAPTURE);

,解释如下: http://www.php.net/manual/en/function.preg-split.php#94238

well i had the same problem in the past. You have to parenthese your regexp with brackets and then it hopefully works

$parts = preg_split('/(\+|>|~|@)/', $input, PREG_SPLIT_OFFSET_CAPTURE);

and here is it explained: http://www.php.net/manual/en/function.preg-split.php#94238

韵柒 2024-11-10 23:39:13

本是对的。

只是为了补充他的答案,PREG_SPLIT_DELIM_CAPTURE 是一个值为 2 的常量,因此您会得到 2 个分割,同样 PREG_SPLIT_OFFSET_CAPTURE 的值为 4。

Ben is correct.

Just to add to his answer, PREG_SPLIT_DELIM_CAPTURE is a constant with value of 2 so you get 2 splits, similarly PREG_SPLIT_OFFSET_CAPTURE has a value of 4.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文