正则取字符串

 

    1. $str="你好<我>(爱)[北京]{天安门}";
    2.  
       
    3.  
      echo f1($str); //返回你好
    4.  
      echo f2($str); //返回我
    5.  
      echo f3($str); //返回爱
    6.  
      echo f4($str); //返回北京
    7.  
      echo f5($str); //返回天安门
    8.  
       
    9.  
      function f1($str)
    10.  
      {
    11.  
      $result = array();
    12.  
      preg_match_all("/^(.*)(?:<)/i",$str, $result);
    13.  
      return $result[1][0];
    14.  
      }
    15.  
       
    16.  
      function f2($str)
    17.  
      {
    18.  
      $result = array();
    19.  
      preg_match_all("/(?:<)(.*)(?:>)/i",$str, $result);
    20.  
      return $result[1][0];
    21.  
      }
    22.  
       
    23.  
      function f3($str)
    24.  
      {
    25.  
      $result = array();
    26.  
      preg_match_all("/(?:\()(.*)(?:\))/i",$str, $result);
    27.  
      return $result[1][0];
    28.  
      }
    29.  
       
    30.  
      function f4($str)
    31.  
      {
    32.  
      $result = array();
    33.  
      preg_match_all("/(?:\[)(.*)(?:\])/i",$str, $result);
    34.  
      return $result[1][0];
    35.  
      }
    36.  
       
    37.  
      function f5($str)
    38.  
      {
    39.  
      $result = array();
    40.  
      preg_match_all("/(?:\{)(.*)(?:\})/i",$str, $result);
    41.  
      return $result[1][0];
    42.  
      }
posted @ 2018-09-14 16:15  千载白云  阅读(94)  评论(0编辑  收藏  举报