Re: seperate streetname from number
| From: | Paul Chvostek | Date: | Tue, 11 Mar 2003 17:58:43 +0000 |
| Subject: | Re: seperate streetname from number | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-139157@lists.php.net to get a copy of this message | ||
On Tue, Mar 11, 2003 at 05:17:52PM +0100, André Sannerholt wrote:
> Hi everyone!
Hi André.
> I wondered how to sepearate string variables that contain streetnames and
> numbers:
>
> 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...
You probably also want the function to recognize addresses in which the
number precedes the street name (Canada, US, Britain, etc). You also
want to support multiple-word streetnames, which would not work if you
explode using a space as seperator. This is untested:
function splitstreetaddress($what) {
if (ereg('[^0-9][0-9]+$', $what)) {
$temp = ereg_replace('(.*[^0-9])([0-9]+)$','\\1__\\2', $what);
list($name, $number)=explode('__', $temp);
} else if (ereg('^[0-9]+', $what)) {
$temp = ereg_replace('^([0-9]+)(.*)','\\1__\\2', $what);
list($number, $name)=explode('__', $temp);
} else {
$name=$what;
$number=""
}
$retval[0]=trim($name);
$retval[1]=trim($number);
return $retval;
}
--
Paul Chvostek <paul@it.ca>
Operations / Abuse / Whatever
it.canada, hosting and development http://www.it.ca/