Re: regex frustration
| From: | Simon Garner | Date: | Wed, 28 Feb 2001 22:11:12 +0000 |
| Subject: | Re: regex frustration | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-42090@lists.php.net to get a copy of this message | ||
From: "Jerry Lake" <jerryl@europa.com>
> I've tried and tried to no avail, can someone assist
> I need to select the first space in a line like the following
> Nelson Bob and Mary, 123 Street st., Ashton, 555-1212
>
> I need to replace the space between Nelson and Bob with
> a comma so I can separate the first and last names in my DB
> ^\D*\s will select the first name and the space, but I just
> need the space. any ideas?
>
<?php
$str = "Nelson Bob and Mary, 123 Street st., Ashton, 555-1212";
$str = ereg_replace("^([^ ]+) (.+)", "\\1, \\2", $str);
?>
Or alternatively:
<?php
$str = "Nelson Bob and Mary, 123 Street st., Ashton, 555-1212";
$str_bits = explode(" ", $str);
$str_bits[0] .= ",";
$str = implode(" ", $str_bits);
?>
Cheers
Simon Garner