Home > Backend Development > PHP Tutorial > 继续求教正则表达式问题

继续求教正则表达式问题

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 14:10:13
Original
905 people have browsed it

$str = "昨天 @测试 hiaaaa dd@测试 反反复复";$reg = "/([^@]+(?=\s))/";$arr = preg_split($reg,$str,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );print_r($arr);
Copy after login


结果是
Array ( [0] => 昨天 [1] => @ [2] => 测试 [3] => hiaaaa [4] => dd@ [5] => 测试 [6] => 反反复复 ) 
Copy after login


想要的结果是

Array ( [0] => 昨天 @ [1] => 测试 [2] => hiaaaa dd@ [3] => 测试 [4] => 反反复复 ) 
Copy after login


求教该如何修改正则?

==============
其实就是想把类似于微博里边@功能用正则来拆出@的人,规则是"@开头+人名+空格"不如此的话 有没有其他更好的实现方法?

如果用这个 类似于
$str = "昨天 @测试 hiaaaa dd@测试 反反复复@好友2";
Copy after login

这样在末尾@了好友,但是没有空格能否用正则判断?


回复讨论(解决方案)

$str = "昨天 @测试 hiaaaa dd@测试 反反复复@好友2";$reg = "/(@\S+)/";$arr = preg_split($reg,$str,-1,PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );print_r($arr);
Copy after login
Array
(
    [0] => 昨天 
    [1] => @测试
    [2] =>  hiaaaa dd
    [3] => @测试
    [4] =>  反反复复
    [5] => @好友2
)

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template