有如下字符串:
"<str1>name1</str1><str2>name2</str2>"
用正则表达式如何取出"name1"和"name2"呢?谢谢

解决方案 »

  1.   

    <?php
    $str = "<str1>name1</str1><str2>name2</str2>";
    $patten  = "/<str\d+>([^<]*)<\/str\d+>/isU";
    preg_match_all($patten,$str,$matches);
    print_r($matches[1]);
      

  2.   


    $str = "<str1>name1</str1><str2>name2</str2>";
    preg_match_all('/>([^<]+)</U', $str, $matches);
    print_r($matches[1]);
    /**
    输出结果:
    Array ( [0] => name1 [1] => name2 ) 
    */
      

  3.   

    preg_match_all("/>[^<]+</",$str,$m);
    print_R($m[1]);
      

  4.   

    $str = "<str1>name1</str1><str2>name2</str2>";
    preg_match_all('/>([^<]+)</U', $str, $matches);
    print_r($matches[1]);
    /**
    输出结果:
    Array ( [0] => name1 [1] => name2 ) 
    */
      

  5.   


    <?php
            $str="<str1>name1</str1><str2>name2</str2>";
    $preg="#<str1>(.*)</str1><str2>(.*)</str2>#";
    preg_match($preg,$str,$arr);
    echo $arr[1];
    echo $arr[2];
    ?>
      

  6.   

    <?php
        $str="<str1>name1</str1><str2>name2</str2>";
        preg_match_all("/<[a-z0-9]*>([^<|>]*)<\/[a-z0-9]*>/",$str,$matches);
        echo $matches[1][0];
        echo $matches[1][1];
    ?>
      

  7.   

    <?php
      $str="<str1>name1</str1><str2>name2</str2>";
      preg_match_all("/<[a-z0-9]*>([^<|>]*)<\/[a-z0-9]*>/",$str,$matches);
      echo $matches[1][0];
      echo $matches[1][1];
    ?>