Guest User

Untitled

a guest
Jan 17th, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.73 KB | None | 0 0
  1. <?php
  2.  
  3. $str = '6 bedroom ground floor flat in Edinburgh
  4.  
  5. house in Manchester
  6.  
  7. 3 bedroom apartment
  8.  
  9. ';
  10.  
  11. //preg_match_all("#(((\d+)\s+bedroom)\s+)?(.+?)\s(in\s+(\w+))?\n#i", $str, $m);
  12. preg_match_all("#(((?<bedroomCount>\d+)\s+bedroom)\s+)?(?<type>.+?)\s(in\s+(?<city>\w+))?\n#i", $str, $m);
  13. var_dump($m);
  14.  
  15.  
  16. /*
  17. array(10) {
  18. [0]=>
  19. array(3) {
  20. [0]=>
  21. string(41) "6 bedroom ground floor flat in Edinburgh
  22. "
  23. [1]=>
  24. string(20) "house in Manchester
  25. "
  26. [2]=>
  27. string(21) "3 bedroom apartment
  28.  
  29. "
  30. }
  31. [1]=>
  32. array(3) {
  33. [0]=>
  34. string(10) "6 bedroom "
  35. [1]=>
  36. string(0) ""
  37. [2]=>
  38. string(10) "3 bedroom "
  39. }
  40. [2]=>
  41. array(3) {
  42. [0]=>
  43. string(9) "6 bedroom"
  44. [1]=>
  45. string(0) ""
  46. [2]=>
  47. string(9) "3 bedroom"
  48. }
  49. ["bedroomCount"]=>
  50. array(3) {
  51. [0]=>
  52. string(1) "6"
  53. [1]=>
  54. string(0) ""
  55. [2]=>
  56. string(1) "3"
  57. }
  58. [3]=>
  59. array(3) {
  60. [0]=>
  61. string(1) "6"
  62. [1]=>
  63. string(0) ""
  64. [2]=>
  65. string(1) "3"
  66. }
  67. ["type"]=>
  68. array(3) {
  69. [0]=>
  70. string(17) "ground floor flat"
  71. [1]=>
  72. string(5) "house"
  73. [2]=>
  74. string(9) "apartment"
  75. }
  76. [4]=>
  77. array(3) {
  78. [0]=>
  79. string(17) "ground floor flat"
  80. [1]=>
  81. string(5) "house"
  82. [2]=>
  83. string(9) "apartment"
  84. }
  85. [5]=>
  86. array(3) {
  87. [0]=>
  88. string(12) "in Edinburgh"
  89. [1]=>
  90. string(13) "in Manchester"
  91. [2]=>
  92. string(0) ""
  93. }
  94. ["city"]=>
  95. array(3) {
  96. [0]=>
  97. string(9) "Edinburgh"
  98. [1]=>
  99. string(10) "Manchester"
  100. [2]=>
  101. string(0) ""
  102. }
  103. [6]=>
  104. array(3) {
  105. [0]=>
  106. string(9) "Edinburgh"
  107. [1]=>
  108. string(10) "Manchester"
  109. [2]=>
  110. string(0) ""
  111. }
  112. }
Add Comment
Please, Sign In to add comment