[正则]分组匹配求解 本帖最后由 MoontoC 于 2011-06-23 12:14:37 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 preg_match('/<\w (\w+), (\w+), (\w+), (\w+)>/i', $in, $matches);var_dump($matches); $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"} $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"}*/ 感谢楼上两位的preg_match_all结果, 虽然是有效, 但没办法对应我的实际情况, 我并不是就像匹配一堆东西里面用逗号分隔的一坨, 而是<tag xxx, xxx, xxx>标签是固定的, 并且有多个不同类型, 不同语法的tag然后还要$rules = '#\{' . join( '\}|\{', $rules ) . '\}#i';最后用preg_replace_callback来连续处理多个tag内容, 所以没办法使用_all, 上面的例子, 我可能说的不全面吧_- 在线投票系统出现的一点小问题 select option选项内容显示 上传到文件夹里的图片怎么显示在网页上啊? php中关于PDO的批量删除问题 js 中的对象是不是就是类的意思 国际语言转换 php 怎么发送邮件 怎样把带参数的php嵌入到当前页中呢? 关于屏蔽代码的问题。 请教MYSQL_RESULT()函数! 请教apache运行PHP的问题 个人PHP技术展示,请指教。。。
preg_match('/<\w (\w+), (\w+), (\w+), (\w+)>/i', $in, $matches);
var_dump($matches);
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"
}
$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"
}
*/