preg_split_basic.phpt 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. --TEST--
  2. Test preg_split() function : basic functionality
  3. --FILE--
  4. <?php
  5. /*
  6. * proto array preg_split(string pattern, string subject [, int limit [, int flags]])
  7. * Function is implemented in ext/pcre/php_pcre.c
  8. */
  9. $string = 'this is a_list: value1, Test__, string; Hello, world!_(parentheses)';
  10. var_dump(preg_split('/[:,;\(\)]/', $string, -1, PREG_SPLIT_NO_EMPTY)); //parts of $string separated by : , ; ( or ) are put into an array.
  11. var_dump(preg_split('/:\s*(\w*,*\s*)+;/', $string)); //all text between : and ; is removed
  12. var_dump(preg_split('/(\(|\))/', $string, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY)); //all text before (parentheses) is put into first element, ( into second, "parentheses" into third and ) into fourth.
  13. var_dump(preg_split('/NAME/i', $string)); //tries to find NAME regardless of case in $string (can't split it so just returns how string as first element)
  14. var_dump(preg_split('/\w/', $string, -1, PREG_SPLIT_NO_EMPTY)); //every character (including whitespace) is put into an array element
  15. ?>
  16. --EXPECT--
  17. array(7) {
  18. [0]=>
  19. string(14) "this is a_list"
  20. [1]=>
  21. string(7) " value1"
  22. [2]=>
  23. string(7) " Test__"
  24. [3]=>
  25. string(7) " string"
  26. [4]=>
  27. string(6) " Hello"
  28. [5]=>
  29. string(8) " world!_"
  30. [6]=>
  31. string(11) "parentheses"
  32. }
  33. array(2) {
  34. [0]=>
  35. string(14) "this is a_list"
  36. [1]=>
  37. string(28) " Hello, world!_(parentheses)"
  38. }
  39. array(4) {
  40. [0]=>
  41. string(54) "this is a_list: value1, Test__, string; Hello, world!_"
  42. [1]=>
  43. string(1) "("
  44. [2]=>
  45. string(11) "parentheses"
  46. [3]=>
  47. string(1) ")"
  48. }
  49. array(1) {
  50. [0]=>
  51. string(67) "this is a_list: value1, Test__, string; Hello, world!_(parentheses)"
  52. }
  53. array(10) {
  54. [0]=>
  55. string(1) " "
  56. [1]=>
  57. string(1) " "
  58. [2]=>
  59. string(2) ": "
  60. [3]=>
  61. string(2) ", "
  62. [4]=>
  63. string(2) ", "
  64. [5]=>
  65. string(2) "; "
  66. [6]=>
  67. string(2) ", "
  68. [7]=>
  69. string(1) "!"
  70. [8]=>
  71. string(1) "("
  72. [9]=>
  73. string(1) ")"
  74. }