token_get_all_variation19.phpt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. --TEST--
  2. Reconstructing a script using token_get_all()
  3. --SKIPIF--
  4. <?php if (!extension_loaded("tokenizer")) print "skip"; ?>
  5. --FILE--
  6. <?php
  7. $phpstr = '
  8. <?php
  9. // A php script to test token_get_all()
  10. /* a different
  11. type of
  12. comment */
  13. // a class
  14. class TestClass {
  15. public function foo() {
  16. echo "Called foo()\n";
  17. }
  18. }
  19. $a = new TestClass();
  20. $a->foo();
  21. for ($i = 0; $i < 10; $i++) {
  22. echo "Loop iteration $i\n";
  23. }
  24. ?>';
  25. $token_array = token_get_all($phpstr);
  26. $script = "";
  27. // reconstruct a script (without open/close tags) from the token array
  28. foreach ($token_array as $token) {
  29. if (is_array($token)) {
  30. if (strncmp($token[1], '<?php', 5) == 0) {
  31. continue;
  32. }
  33. if (strncmp($token[1], '?>', 2) == 0) {
  34. continue;
  35. }
  36. $script .= $token[1];
  37. } else {
  38. $script .= $token;
  39. }
  40. }
  41. var_dump($script);
  42. eval($script);
  43. ?>
  44. --EXPECT--
  45. string(259) "
  46. // A php script to test token_get_all()
  47. /* a different
  48. type of
  49. comment */
  50. // a class
  51. class TestClass {
  52. public function foo() {
  53. echo "Called foo()\n";
  54. }
  55. }
  56. $a = new TestClass();
  57. $a->foo();
  58. for ($i = 0; $i < 10; $i++) {
  59. echo "Loop iteration $i\n";
  60. }
  61. "
  62. Called foo()
  63. Loop iteration 0
  64. Loop iteration 1
  65. Loop iteration 2
  66. Loop iteration 3
  67. Loop iteration 4
  68. Loop iteration 5
  69. Loop iteration 6
  70. Loop iteration 7
  71. Loop iteration 8
  72. Loop iteration 9