3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php function splitStreetNameNumber($str, $numRequired = false) { $str = trim($str); $tmp = @preg_split('/[0-9]+/', $str); if ( (0 >= strlen($str) || 0 >= strlen(trim($tmp[0]))) || ($numRequired && 0 >= strlen(trim($tmp[1]))) ) { return false; } return array( 'name' => trim($tmp[0]), 'num' => trim(str_replace(trim($tmp[0]), '', $str)), ); } $a = array( 'Karlstr. 2', '12345', 'Karl Straße 2d', 'Karlsplatz 14 a', '', 'Karlsplatz', ); foreach($a as $b) { $c = splitStreetNameNumber($b); if (!$c) { echo "Not a valid address.\r\n"; } else { printf("Streetname: '%s', Streetnumber: '%s'.\r\n", $c['name'], $c['num']); } }
Output for 4.3.0 - 5.6.28, hhvm-3.10.0 - 3.12.0, 7.0.0 - 7.1.0
Streetname: 'Karlstr.', Streetnumber: '2'. Not a valid address. Streetname: 'Karl Straße', Streetnumber: '2d'. Streetname: 'Karlsplatz', Streetnumber: '14 a'. Not a valid address. Streetname: 'Karlsplatz', Streetnumber: ''.