$str = '<p:foreach items="111" var="222">333 </p:foreach>';
preg_match_all('/\d+/',$str,$out);
print_r($out);

解决方案 »

  1.   

    $str = '<p:foreach items="111" var="222">333 </p:foreach>'; 
    preg_match_all('/<p:foreach items\s*=\s*[\"\']??([^\"\']+)\s*[\"\']??\s*var\s*=\s*[\"\']??([^\"\']+)\s*[\"\']??>([^<]+)<\/p:foreach>/is',$str,$matchs); echo "match all:",htmlspecialchars($matchs[0][0]),"<br/>";$cnt = count($matchs);for($i=1;$i<$cnt;$i++){
    echo "match $i:",$matchs[$i][0],"<br/>";
    }
    注意,match all为整个匹配项,因为匹配下来在一个标签内,所以要使用htmlspecialchars函数才能看到源代码,否则只能看到一个333
      

  2.   

    我这个正则表达式比较长,因为考虑的比较严谨。例如items=的等号之间是否有空格,例如items="你使用的是单引号或是双引号。都可以匹配。如果你的html源代码不是很规则,建议采用我的。