本帖最后由 edisonli 于 2012-10-16 10:57:17 编辑

解决方案 »

  1.   

    <?php
    $string = 'some text <tag:cate name="a">a<tag:cate name="b">b<tag:cate>c</tag:cate>d</tag:cate>e</tag:cate> more text';
    preg_match("/<tag:cate(?: name=\"\w+\")?>(?>\w+|(?R))*<\/tag:cate>/",$string,$matches) &&
    print_r($matches);
      

  2.   

    $string = 'some text <tag:cate name="a"><div></div><tag:cate name="b"><div></div><tag:cate>c</tag:cate>d</tag:cate>e</tag:cate> more text'; 
    if(preg_match('#<tag:cate name="\w+">(.*)</tag:cate>#i',$string,$matches)) 

    echo "<pre>"; print_r($matches); echo "</pre>"; 
      

  3.   


    不好意思,刚刚问题说漏了一点,<tag:cate name="a"></tag:cate>之间不一定是问题,有可能是HTML标签,比如$string = 'some text <tag:cate name="a"><div>a</div><tag:cate name="b"><div></div><tag:cate>c</tag:cate>d</tag:cate>e</tag:cate> more text';
      

  4.   

    <?php
    $string = 'some text <tag:cate name="a"><div>a</div><tag:cate name="b"><div></div><tag:cate>c</tag:cate>d</tag:cate>e</tag:cate> more text';
    preg_match("/<tag:cate(?: name=\"\w+\")?>(?>(?:<\/?\w+>)|\w+|(?R))*<\/tag:cate>/",$string,$matches) &&
    print_r($matches);
      

  5.   

    各位大虾,不好意思,我的问题发错了一点
    $string = 'some text <tag:cate name="a"><div></div><tag:cate name="b"><div></div><tag:cate>c</tag:cate>d<tag:cate>e</tag:cate> more text';我想匹配为
    <tag:cate name="a"><div></div><tag:cate name="b"><div></div><tag:cate>c</tag:cate>
    <tag:cate>e</tag:cate>string中的<tag:cate></tag:cate>个数不定,大虾们拜托再帮帮忙,非常感谢