PHP:split / explode不返回结果数组中的分隔符

时间:2022-05-08 22:04:22

Current string: "m, d - y".

当前字符串:“m,d - y”。

I want to split / explode it by d, m and y.

我想用d,m和y分割/爆炸它。

After processing I should get:

处理后我应该得到:

array[0] = "m";
array[1] = ", ";
array[2] = "d";
array[3] = " - ";
array[4] = "y";

How to do this? Split and explode are removing my delimiters from the returned array.

这个怎么做?拆分和爆炸正在从返回的数组中删除我的分隔符。

4 个解决方案

#1


1  

Just split the input according to the boundary which exists before (d or m or y ) or the boundary which exists after d or m or y

只需根据(d或m或y)之前存在的边界或d或m或y之后存在的边界分割输入

$str = "m, d - y";
$match = preg_split('~(?<=[dmy])(?!$)|(?<!^)(?=[dmy])~m', $str);
print_r($match);

Output:

输出:

Array
(
    [0] => m
    [1] => , 
    [2] => d
    [3] =>  - 
    [4] => y
)

Explanation:

说明:

  • (?<=[dmy]) immediate look after to [dmy] and match (?!$) won't be followed by end of the line. This would avoid matching the boundary exists at the last.

    (?<= [dmy])立即关注[dmy]并且匹配(?!$)后面不会跟着行结束。这样可以避免匹配最后存在的边界。

  • (?=[dmy]) matches all the boundaries which exists before d or m or y but the match won't be at the start of aline.

    (?= [dmy])匹配d或m或y之前存在的所有边界,但匹配不在aline的开头。

#2


0  

You can use preg_match_all and match the delimiters and those, except delimiters with any spaces surrounding them.

你可以使用preg_match_all并匹配分隔符和那些分隔符,除了分隔符和它们周围的任何空格。

Use the regex /[dmy]|\s*[^dmy]\s*/

使用正则表达式/ [dmy] | \ s * [^ dmy] \ s * /

#3


0  

preg_split supports the option PREG_SPLIT_DELIM_CAPTURE that includes captured part of the delimiter in the final result.

preg_split支持PREG_SPLIT_DELIM_CAPTURE选项,该选项包括最终结果中捕获的分隔符部分。

$result = preg_split('~([mdy])~', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

PREG_SPLIT_NO_EMPTY option, removes empty strings from the result array.

PREG_SPLIT_NO_EMPTY选项,从结果数组中删除空字符串。

#4


0  

Split and Explode are not meant to return the separator. I'd say best function to use here is preg_match:

拆分和爆炸并不意味着返回分隔符。我想说这里使用的最好的函数是preg_match:

http://php.net/manual/de/function.preg-match.php

http://php.net/manual/de/function.preg-match.php

So you can define bracketed groups of chars that should get "grep'd" into one item of the resulting array.

因此,您可以定义括号内的字符组,这些字符组应该“生成”到结果数组的一个项目中。

The basic idea for the pattern here is to alternate the same group of chars [-, ] with and without the negation ^. Please note that the char - needs an escape \ because of its special meaning in the character class [].

这里模式的基本思想是交替使用相同的字符组[ - ,],有或没有否定^。请注意,char - 需要一个转义\因为它在字符类[]中的特殊含义。

[edit: learned something today: preg_split has a cool option. Thanks to the others mentioning that. Here are now both solutions to compare.]

[编辑:今天学到了一些东西:preg_split有一个很酷的选择。感谢其他人提到这一点。以下是两种比较解决方案。]

<?php

$input = "m, d - y";

preg_match ("/([^ ,\-]+)([,\- ]+)([^ ,\-]+)([,\- ]+)([^ ,\-]+)/", $input, $result);
$result2 = preg_split ("/([\-\s,]+)/", $input, -1, PREG_SPLIT_DELIM_CAPTURE);
print ("<pre>");
var_dump ($result);
var_dump ($result2);
print ("</pre>");

#1


1  

Just split the input according to the boundary which exists before (d or m or y ) or the boundary which exists after d or m or y

只需根据(d或m或y)之前存在的边界或d或m或y之后存在的边界分割输入

$str = "m, d - y";
$match = preg_split('~(?<=[dmy])(?!$)|(?<!^)(?=[dmy])~m', $str);
print_r($match);

Output:

输出:

Array
(
    [0] => m
    [1] => , 
    [2] => d
    [3] =>  - 
    [4] => y
)

Explanation:

说明:

  • (?<=[dmy]) immediate look after to [dmy] and match (?!$) won't be followed by end of the line. This would avoid matching the boundary exists at the last.

    (?<= [dmy])立即关注[dmy]并且匹配(?!$)后面不会跟着行结束。这样可以避免匹配最后存在的边界。

  • (?=[dmy]) matches all the boundaries which exists before d or m or y but the match won't be at the start of aline.

    (?= [dmy])匹配d或m或y之前存在的所有边界,但匹配不在aline的开头。

#2


0  

You can use preg_match_all and match the delimiters and those, except delimiters with any spaces surrounding them.

你可以使用preg_match_all并匹配分隔符和那些分隔符,除了分隔符和它们周围的任何空格。

Use the regex /[dmy]|\s*[^dmy]\s*/

使用正则表达式/ [dmy] | \ s * [^ dmy] \ s * /

#3


0  

preg_split supports the option PREG_SPLIT_DELIM_CAPTURE that includes captured part of the delimiter in the final result.

preg_split支持PREG_SPLIT_DELIM_CAPTURE选项,该选项包括最终结果中捕获的分隔符部分。

$result = preg_split('~([mdy])~', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

PREG_SPLIT_NO_EMPTY option, removes empty strings from the result array.

PREG_SPLIT_NO_EMPTY选项,从结果数组中删除空字符串。

#4


0  

Split and Explode are not meant to return the separator. I'd say best function to use here is preg_match:

拆分和爆炸并不意味着返回分隔符。我想说这里使用的最好的函数是preg_match:

http://php.net/manual/de/function.preg-match.php

http://php.net/manual/de/function.preg-match.php

So you can define bracketed groups of chars that should get "grep'd" into one item of the resulting array.

因此,您可以定义括号内的字符组,这些字符组应该“生成”到结果数组的一个项目中。

The basic idea for the pattern here is to alternate the same group of chars [-, ] with and without the negation ^. Please note that the char - needs an escape \ because of its special meaning in the character class [].

这里模式的基本思想是交替使用相同的字符组[ - ,],有或没有否定^。请注意,char - 需要一个转义\因为它在字符类[]中的特殊含义。

[edit: learned something today: preg_split has a cool option. Thanks to the others mentioning that. Here are now both solutions to compare.]

[编辑:今天学到了一些东西:preg_split有一个很酷的选择。感谢其他人提到这一点。以下是两种比较解决方案。]

<?php

$input = "m, d - y";

preg_match ("/([^ ,\-]+)([,\- ]+)([^ ,\-]+)([,\- ]+)([^ ,\-]+)/", $input, $result);
$result2 = preg_split ("/([\-\s,]+)/", $input, -1, PREG_SPLIT_DELIM_CAPTURE);
print ("<pre>");
var_dump ($result);
var_dump ($result2);
print ("</pre>");