PHP正則関数の使い方

4388 ワード

          ,                 ,       php             ,         。 ,      php             。
php           preg  ,         :
1,preg_grep
2,preg_match
3,preg_match_all
4,preg_quote
5,preg_replace
6,preg_repalce_callback
7,preg_spilt

preg_grep --              
preg_grep     input         pattern               。
  1.1
<?php
$arr = array('a','1',4,'8','cd','g');
$array = preg_grep ("/^(\d)+$/", $arr);//         。
print_r($array);
?>
  :Array ( [1] => 1 [2] => 4 [3] => 8 )

preg_match --          
  :int preg_match ( string pattern, string subject [, array matches [, int flags]] )  subject         pattern               。    0 1。
  2.1
<?php
echo preg_match ("/^hello/i", "zhe shi hello World");  //      hello  ,echo 0
echo preg_match ("/^hello/i", "hello World");          //      hello  ,echo 1
?>
  01


preg_match_all --            
  :int preg_match_all ( string pattern, string subject, array matches [, int flags] )  subject        pattern                    flags         matches  。     。
  3.1
<?php
$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all ("/(<([\w]+)[^>]*>)(.*)(<\/\\2>)/", $html, $matches);
print_r($matches);
?>
  :
Array ( [0] => Array ( [0] => <b>bold text</b> [1] => <a href=howdy.html>click me</a> )
        [1] => Array ( [0] => <b> [1] => <a href=howdy.html> )
        [2] => Array ( [0] => b [1] => a )
        [3] => Array ( [0] => bold text [1] => click me )
        [4] => Array ( [0] => </b> [1] => </a>)
)
         ,           $matches[$i][0] ,         $matches[$i][1] ,  $matches[0]        ,$matches[1]               :(<([\w]+)[^>]*>),$matches[2]               :([\w]+),$matches[3]               :(.*),$matches[4]               :(<\/\\2>)【 :\\2         

       preg_grep,preg_match,preg_match_all       ,                。
preg_quote --          
  :string preg_quote ( string str [, string delimiter] )preg_quote()   str                               。                                         。
  4.1
<?php
$s = "This's a test,'ha ha'";
$s = preg_quote ($s, "'");
echo $s;
?>
  :This\'s a test,\'ha ha\'


preg_replace --              
mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit] )  subject     pattern            replacement。      limit,     limit    ,     limit       -1,            。
  5.1
<?php
$string = "2010-10-11";
$pattern = "/(\d+)-(\d+)-(\d+)/i";
$replacement = "\${2},\${3} \${1}";
print preg_replace($pattern, $replacement, $string);
?>
  :10,11 2010


preg_split --            
  :array preg_split ( string pattern, string subject [, int limit [, int flags]] )      ,   subject      pattern            。
  6.1
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);    //       
print_r($chars);
?>
  :Array (
            [0] => Array ( [0] => hypertext [1] => 0 )
            [1] => Array ( [0] => language [1] => 10 )
            [2] => Array ( [0] => programming [1] => 19 )
)


preg_replace_callback --                   
  :mixed preg_replace_callback ( mixed pattern, callback callback, mixed subject [, int limit] )         preg_replace  ,         replacement   ,       callback   。                      ,           。
<?php
  $text = "Fri Dec 25 13:07:17 +0800 2009";
  function call_back($matches) {
      $arr = array('Jan ','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec');
    $r_arr = array('1','2','3','4','5','6','7','8','9','10','11','12');
    $date_time = str_replace($arr,$r_arr,$matches['2']);
    return $matches[6].'-'.$date_time.'-'.$matches[3].' '.$matches[4];
  }

  echo preg_replace_callback(
  "|(\w+)\s(\w+)\s(\w+)\s([\w:]+)\s([\w\+]+)\s(\w+)|",
   "call_back",$text);
?>
  :2009-12-25 13:07:17