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 && (isset($tmp[1]) || 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) { 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: ''. */ } 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 5.3.0 - 5.6.28, 7.0.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': Not a valid address. Match result for '12345': Not a valid address. Match result for 'Karl Straße 2d': Not a valid address. Match result for 'Karlsplatz 14 a': Not a valid address. Match result for '': Not a valid address. Notice: Undefined offset: 1 in /in/gd3Jh on line 8 Match result for 'Karlsplatz': Not a valid address.
Output for hhvm-3.12.0 - 3.13.2
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': Not a valid address. Match result for '12345': Not a valid address. Match result for 'Karl Straße 2d': Not a valid address. Match result for 'Karlsplatz 14 a': Not a valid address. Match result for '': Not a valid address. Notice: Undefined index: 1 in /in/gd3Jh on line 8 Warning: trim() expects parameter 1 to be string, null given in /in/gd3Jh on line 8 Match result for 'Karlsplatz': Not a valid address.
Output for hhvm-3.10.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': Not a valid address. Match result for '12345': Not a valid address. Match result for 'Karl Straße 2d': Not a valid address. Match result for 'Karlsplatz 14 a': Not a valid address. Match result for '': Not a valid address. Notice: Undefined index: 1 in /in/gd3Jh on line 8 Match result for 'Karlsplatz': Not a valid address.
Output for 4.3.0 - 5.2.17
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': Not a valid address. Match result for '12345': Not a valid address. Match result for 'Karl Straße 2d': Not a valid address. Match result for 'Karlsplatz 14 a': Not a valid address. Match result for '': Not a valid address. Notice: Undefined offset: 1 in /in/gd3Jh on line 8 Match result for 'Karlsplatz': Not a valid address.