Re: seperate streetname from number
| From: | Ernest E Vogelsinger | Date: | Tue, 11 Mar 2003 17:27:16 +0000 |
| Subject: | Re: seperate streetname from number | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139150@lists.php.net to get a copy of this message | ||
At 17:17 11.03.2003, André Sannerholt said:
--------------------[snip]--------------------
>If for example
>$variable="Hauptstraße 15";
>
>$variable_string[0] should be "Hauptstraße"
>$variable_string[1] should be "15"
>
>So I need a function that recognizes a number and explodes the variable at
>that very position.
>It's important that the thing also works when $variable="Hauptstraße15";
>without any space. Else it would have been easy to do with the explode
>function...
--------------------[snip]--------------------
try a preg_match like (untested):
if (preg_match('/^\s*([^0-9]*)\s*([0-9].*)\s*$/', $address, $aresult)) {
$address = $aresult[1];
$number = $aresult[2];
}
else {
$address = trim($address);
$number = null;
}
Note that preg_match will return false here if no number is available in
$address. Leading and trailing spaces are trimmed from $address and
$number, either by the regular expression, or by the trim() function in
case of a no-match.
Note also that this will fail for US-style addresses like "9th street",
moving everything in $number, leaving $address empty.
--
>O Ernest E. Vogelsinger
(\) ICQ #13394035
^ http://www.vogelsinger.at/