preg_split_basic.phpt 1.8 KB

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