3v4l.org

run code in 500+ PHP versions simultaneously
<?php $data = [ 'Wellington New Zealand', 'Florida United States of America', 'Quebec Canada', 'Something Country XYZ (formally ABC)', ]; $countries = [ 'United States of America', 'Canada', 'New Zealand', 'Country XYZ (formally ABC)', ]; $branches = array_map(fn($country) => preg_quote($country, '/'), $countries); $result = []; foreach ($data as $string) { $result[] = preg_split('/ (?=(?:' . implode('|', $branches) . ')$)/', $string, 2); } var_export($result);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.6
array ( 0 => array ( 0 => 'Wellington', 1 => 'New Zealand', ), 1 => array ( 0 => 'Florida', 1 => 'United States of America', ), 2 => array ( 0 => 'Quebec', 1 => 'Canada', ), 3 => array ( 0 => 'Something', 1 => 'Country XYZ (formally ABC)', ), )

preferences:
94.01 ms | 1309 KiB | 4 Q