preg_grep_basic.phpt 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. --TEST--
  2. Test preg_grep() function : basic functionality
  3. --FILE--
  4. <?php
  5. /*
  6. * Function is implemented in ext/pcre/php_pcre.c
  7. */
  8. $array = array('HTTP://WWW.EXAMPLE.COM', '/index.html', '/info/stat/', 'http://test.uk.com/index/html', '/display/dept.php');
  9. var_dump($array);
  10. var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array)); //finds a string starting with http (regardless of case) (matches two)
  11. var_dump(preg_grep('@(/\w+\.*/*)+@', $array)); //finds / followed by one or more of a-z, A-Z and 0-9, followed by zero or more . followed by zero or more / all more than once. (matches all)
  12. var_dump(preg_grep('@^http://[^w]{3}.*$@i', $array)); //finds http:// (at the beginning of a string) not followed by 3 characters that aren't w's then anything to the end of the sttring (matches one)
  13. var_dump(preg_grep('@.*?\.co\.uk$@i', $array)); //finds any address ending in .co.uk (matches none)
  14. var_dump(preg_grep('@^HTTP(.*?)\w{2,}$@i', $array, PREG_GREP_INVERT)); //same as first example but the array created contains everything that is NOT matched but the regex (matches three)
  15. ?>
  16. --EXPECT--
  17. array(5) {
  18. [0]=>
  19. string(22) "HTTP://WWW.EXAMPLE.COM"
  20. [1]=>
  21. string(11) "/index.html"
  22. [2]=>
  23. string(11) "/info/stat/"
  24. [3]=>
  25. string(29) "http://test.uk.com/index/html"
  26. [4]=>
  27. string(17) "/display/dept.php"
  28. }
  29. array(2) {
  30. [0]=>
  31. string(22) "HTTP://WWW.EXAMPLE.COM"
  32. [3]=>
  33. string(29) "http://test.uk.com/index/html"
  34. }
  35. array(5) {
  36. [0]=>
  37. string(22) "HTTP://WWW.EXAMPLE.COM"
  38. [1]=>
  39. string(11) "/index.html"
  40. [2]=>
  41. string(11) "/info/stat/"
  42. [3]=>
  43. string(29) "http://test.uk.com/index/html"
  44. [4]=>
  45. string(17) "/display/dept.php"
  46. }
  47. array(1) {
  48. [3]=>
  49. string(29) "http://test.uk.com/index/html"
  50. }
  51. array(0) {
  52. }
  53. array(3) {
  54. [1]=>
  55. string(11) "/index.html"
  56. [2]=>
  57. string(11) "/info/stat/"
  58. [4]=>
  59. string(17) "/display/dept.php"
  60. }