21,887
社区成员
发帖
与我相关
我的任务
分享
$in = '<x xx, x1, x2, x3>';
preg_match('#^<x ([a-z\d]+), ([a-z\d]+)(?:, ([a-z\d]+))*>$#i', $in, $matches);
var_dump($matches);
array(4) {
[0]=> string(18) ""
[1]=> string(2) "xx"
[2]=> string(2) "x1"
[3]=> string(2) "x3" }
array(4) {
[0]=> string(18) ""
[1]=> string(2) "xx"
[2]=> string(2) "x1"
[3]=> string(2) "x2"
[4]=> string(2) "x3" }
preg_match('/<\w (\w+), (\w+), (\w+), (\w+)>/i', $in, $matches);
var_dump($matches);
$str = <<<TEXT
<x xx, x1, x2, x3>
TEXT;
preg_match_all('#([a-z\d]+,?)+#i', $str, $matches);
echo '<pre>';var_dump($matches[1]);
/*
输出结果:
array(5) {
[0]=>
string(1) "x"
[1]=>
string(3) "xx,"
[2]=>
string(3) "x1,"
[3]=>
string(3) "x2,"
[4]=>
string(2) "x3"
}
*/
$in = '<x xx, x1, x2, x3>';
preg_match_all('/(\w+){2,}/i', $in, $matches);
var_dump($matches[0]);
array(4) {
[0]=>
string(2) "xx"
[1]=>
string(2) "x1"
[2]=>
string(2) "x2"
[3]=>
string(2) "x3"
}