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); $num = trim(str_replace(trim($tmp[0]), '', $str)); if ( 0 >= strlen($str) || 0 >= strlen(trim($tmp[0])) || ($numRequired && 0 >= strlen($num)) ) { 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) { printf("Match result for '%s': Not a valid address.\r\n", $b); } else { printf("Match result for '%s': Streetname: '%s', Streetnumber: '%s'.\r\n", $b, $c['name'], $c['num']); } /* Prints: * 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: ''. */ } print "\r\n--------------------------------\r\n\r\n"; foreach($a as $b) { $c = splitStreetNameNumber($b, true); if (!$c) { printf("Match result for '%s': Not a valid address.\r\n", $b); } else { printf("Match result for '%s': Streetname: '%s', Streetnumber: '%s'.\r\n", $b, $c['name'], $c['num']); } /* Prints: * Streetname: 'Karlstr.', Streetnumber: '2'. * Not a valid address. * Streetname: 'Karl Straße', Streetnumber: '2d'. * Streetname: 'Karlsplatz', Streetnumber: '14 a'. * Not a valid address. * Not a valid address. */ }
Output for 4.3.0 - 7.1.0
Match result for 'Karlstr. 2': Streetname: 'Karlstr.', Streetnumber: '2'. Match result for '12345': Not a valid address. Match result for 'Karl Straße 2d': Streetname: 'Karl Straße', Streetnumber: '2d'. Match result for 'Karlsplatz 14 a': Streetname: 'Karlsplatz', Streetnumber: '14 a'. Match result for '': Not a valid address. Match result for 'Karlsplatz': Streetname: 'Karlsplatz', Streetnumber: ''. -------------------------------- Match result for 'Karlstr. 2': Streetname: 'Karlstr.', Streetnumber: '2'. Match result for '12345': Not a valid address. Match result for 'Karl Straße 2d': Streetname: 'Karl Straße', Streetnumber: '2d'. Match result for 'Karlsplatz 14 a': Streetname: 'Karlsplatz', Streetnumber: '14 a'. Match result for '': Not a valid address. Match result for 'Karlsplatz': Not a valid address.