3v4l.org

run code in 300+ PHP versions simultaneously
<?php $string = " Name: John Doe Address: 123 Main St Second Address: hola City: Chicago State: IL Zip: 60021 Name: Jane Smith Address: 123 State Ave City: Boston State: MA Zip: 02501 Name: John Doe Address: 123 Main St City: Chicago State: IL Zip: 60021"; preg_match_all('!(Name.*|Address.*|City.*|State.*|Zip.*)!', $string, $results); $records = $results[0]; $finalResults = []; $lastElement = "Zip"; foreach ($records as $record) { $result[] = $record; $recordArray = explode(":", $record); if ($recordArray[0] === $lastElement) { $finalResults[] = $result; $result = []; } } var_dump($finalResults);
Output for 7.0.0 - 7.0.23, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
array(3) { [0]=> array(6) { [0]=> string(14) "Name: John Doe" [1]=> string(20) "Address: 123 Main St" [2]=> string(13) "Address: hola" [3]=> string(13) "City: Chicago" [4]=> string(9) "State: IL" [5]=> string(10) "Zip: 60021" } [1]=> array(5) { [0]=> string(16) "Name: Jane Smith" [1]=> string(22) "Address: 123 State Ave" [2]=> string(12) "City: Boston" [3]=> string(9) "State: MA" [4]=> string(10) "Zip: 02501" } [2]=> array(5) { [0]=> string(14) "Name: John Doe" [1]=> string(20) "Address: 123 Main St" [2]=> string(13) "City: Chicago" [3]=> string(9) "State: IL" [4]=> string(10) "Zip: 60021" } }

preferences:
121.73 ms | 405 KiB | 224 Q