Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $str = '6 bedroom ground floor flat in Edinburgh
- house in Manchester
- 3 bedroom apartment
- ';
- //preg_match_all("#(((\d+)\s+bedroom)\s+)?(.+?)\s(in\s+(\w+))?\n#i", $str, $m);
- preg_match_all("#(((?<bedroomCount>\d+)\s+bedroom)\s+)?(?<type>.+?)\s(in\s+(?<city>\w+))?\n#i", $str, $m);
- var_dump($m);
- /*
- array(10) {
- [0]=>
- array(3) {
- [0]=>
- string(41) "6 bedroom ground floor flat in Edinburgh
- "
- [1]=>
- string(20) "house in Manchester
- "
- [2]=>
- string(21) "3 bedroom apartment
- "
- }
- [1]=>
- array(3) {
- [0]=>
- string(10) "6 bedroom "
- [1]=>
- string(0) ""
- [2]=>
- string(10) "3 bedroom "
- }
- [2]=>
- array(3) {
- [0]=>
- string(9) "6 bedroom"
- [1]=>
- string(0) ""
- [2]=>
- string(9) "3 bedroom"
- }
- ["bedroomCount"]=>
- array(3) {
- [0]=>
- string(1) "6"
- [1]=>
- string(0) ""
- [2]=>
- string(1) "3"
- }
- [3]=>
- array(3) {
- [0]=>
- string(1) "6"
- [1]=>
- string(0) ""
- [2]=>
- string(1) "3"
- }
- ["type"]=>
- array(3) {
- [0]=>
- string(17) "ground floor flat"
- [1]=>
- string(5) "house"
- [2]=>
- string(9) "apartment"
- }
- [4]=>
- array(3) {
- [0]=>
- string(17) "ground floor flat"
- [1]=>
- string(5) "house"
- [2]=>
- string(9) "apartment"
- }
- [5]=>
- array(3) {
- [0]=>
- string(12) "in Edinburgh"
- [1]=>
- string(13) "in Manchester"
- [2]=>
- string(0) ""
- }
- ["city"]=>
- array(3) {
- [0]=>
- string(9) "Edinburgh"
- [1]=>
- string(10) "Manchester"
- [2]=>
- string(0) ""
- }
- [6]=>
- array(3) {
- [0]=>
- string(9) "Edinburgh"
- [1]=>
- string(10) "Manchester"
- [2]=>
- string(0) ""
- }
- }
Add Comment
Please, Sign In to add comment