fork download
  1. <?php
  2.  
  3. $regex = '/\d/i';
  4. $result = preg_match($regex , "PHP is 2the w3eb scr4ipti5ng language of choice." );
  5.  
  6. $subject = "dd133aa2";
  7. $pattern = '/\d+/';
  8. preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
  9. print_r($matches);
  10.  
  11. $subject = "dd133aa2";
  12. $pattern = '/\d+/';
  13. preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
  14. print_r($matches);
  15.  
  16. $subject = array("dd133aa2", "kk1ff3");
  17. $pattern = '/\d+/';
  18. $result = preg_replace($pattern, 'Z', $subject);
  19. print_r($result);
  20.  
  21. $subject = "dd133aa2";
  22. $pattern = '/\d+/';
  23. $result = preg_replace_callback($pattern, function($matches) {
  24. //$matches [0] => 133
  25. //$matches [0] => 2
  26. return 'Z';
  27. }, $subject);
  28. //print_r($result);
  29.  
  30. $subject = array("dd133aa2", "kk1ff3", "ddsdfd");
  31. $pattern = '/\d+/';
  32. $result = preg_grep($pattern, $subject);
  33. $result2 = preg_grep($pattern, $subject, PREG_GREP_INVERT);
  34. print_r($result);
  35. print_r($result2);
  36.  
  37. $subject = "dd133aa2cc";
  38. $pattern = '/(\d+)/';
  39. $result = preg_split($pattern, $subject);
  40. $result2 = preg_split($pattern, $subject, null, PREG_SPLIT_DELIM_CAPTURE);
  41. print_r($result);
  42. print_r($result2);
  43.  
  44. preg_match('/(?:\D+|<\d+>)*[!?]/', 'foobar foobar foobar');
  45. $result = preg_last_error();//PREG_BACKTRACK_LIMIT_ERROR 调用回溯限制超出
  46. //print_r($result);
  47.  
  48. $subject = "dd.a?a2cc";
  49. $result = preg_quote($subject);
  50. $result2 = preg_quote($subject, 'a');
  51. print_r($result);
  52. //print_r($result2);
Success #stdin #stdout 0.05s 52472KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => 133
            [1] => 2
        )

)
Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 133
                    [1] => 2
                )

            [1] => Array
                (
                    [0] => 2
                    [1] => 7
                )

        )

)
Array
(
    [0] => ddZaaZ
    [1] => kkZffZ
)
Array
(
    [0] => dd133aa2
    [1] => kk1ff3
)
Array
(
    [2] => ddsdfd
)
Array
(
    [0] => dd
    [1] => aa
    [2] => cc
)
Array
(
    [0] => dd
    [1] => 133
    [2] => aa
    [3] => 2
    [4] => cc
)
dd\.a\?a2cc