Home >Backend Development >PHP Tutorial >关于preg_split(),能否在获得被匹配正则表达式的字符串分割的字符串的同时获得匹配正则表达式的字符串本身?

关于preg_split(),能否在获得被匹配正则表达式的字符串分割的字符串的同时获得匹配正则表达式的字符串本身?

WBOY
WBOYOriginal
2016-06-23 14:09:16907browse

如题

举例:

$reg = "([^@]+(?=\s))";print_r(preg_split($reg,"@张三 你好我是@李四 的朋友@王五不在家",PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_OFFSET_CAPTURE|PREG_SPLIT_NO_EMPTY));


结果
Array(    [0] => @    [1] =>  你好我是@    [2] =>  的朋友@王五不在家)


希望得到的是
Array(    [0] => @    [1] => 张三    [2] =>  你好我是@    [3] => 李四    [4] =>  的朋友@王五不在家)


查手册看到有个参数PREG_SPLIT_DELIM_CAPTURE :如果这个标记设置了,用于分隔的模式中的括号表达式将被捕获并返回。 
但是没有被返回.....


回复讨论(解决方案)

参数位置错了

$reg = "/([^@]+(?=\s))/";print_r(preg_split($reg,"@张三 你好我是@李四 的朋友@王五不在家", -1, PREG_SPLIT_DELIM_CAPTURE |PREG_SPLIT_NO_EMPTY));
Array
(
    [0] => @
    [1] => 张三
    [2] =>  你好我是@
    [3] => 李四
    [4] =>  的朋友@王五不在家
)

非常感?~~~~~~~??了.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn