preg_match_basic.phpt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. --TEST--
  2. Test preg_match() function : basic functionality
  3. --FILE--
  4. <?php
  5. /*
  6. * proto int preg_match(string pattern, string subject [, array subpatterns [, int flags [, int offset]]])
  7. * Function is implemented in ext/pcre/php_pcre.c
  8. */
  9. $string = 'Hello, world. [*], this is \ a string';
  10. var_dump(preg_match('/^[hH]ello,\s/', $string, $match1)); //finds "Hello, "
  11. var_dump($match1);
  12. var_dump(preg_match('/l^o,\s\w{5}/', $string, $match2, PREG_OFFSET_CAPTURE)); // tries to find "lo, world" at start of string
  13. var_dump($match2);
  14. var_dump(preg_match('/\[\*\],\s(.*)/', $string, $match3)); //finds "[*], this is \ a string";
  15. var_dump($match3);
  16. var_dump(preg_match('@\w{4}\s\w{2}\s\\\(?:\s.*)@', $string, $match4, PREG_OFFSET_CAPTURE, 14)); //finds "this is \ a string" (with non-capturing parentheses)
  17. var_dump($match4);
  18. var_dump(preg_match('/hello world/', $string, $match5)); //tries to find "hello world" (should be Hello, world)
  19. var_dump($match5);
  20. ?>
  21. --EXPECTF--
  22. int(1)
  23. array(1) {
  24. [0]=>
  25. string(7) "Hello, "
  26. }
  27. int(0)
  28. array(0) {
  29. }
  30. int(1)
  31. array(2) {
  32. [0]=>
  33. string(23) "[*], this is \ a string"
  34. [1]=>
  35. string(18) "this is \ a string"
  36. }
  37. int(1)
  38. array(1) {
  39. [0]=>
  40. array(2) {
  41. [0]=>
  42. string(18) "this is \ a string"
  43. [1]=>
  44. int(19)
  45. }
  46. }
  47. int(0)
  48. array(0) {
  49. }