RE: [PHP] For the reg ex people
| From: | Richard Heyes | Date: | Mon, 14 Aug 2000 18:02:51 +0000 |
| Subject: | RE: [PHP] For the reg ex people | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-11649@lists.php.net to get a copy of this message | ||
> I'm trying to replace all "-" with any whitespace on either side in a
> string with a regular "-" with not whitespace. Something like this:
>
> "The news today is - something" would end up like
> "The news today is-something"
>
> I don't know how to use regular expressions well enough to do that.
> Could someone help me out?
str_replace(' - ', '-', $string);
Although rather fast, that won't accomodate multiple spaces, this one
will:
$string = preg_replace('/[[:space:]]+-[[:space:]]+/i', '-', $string);
And this one will accomodate multiple spaces, plus the possibility of
only one side having spaces:
$string = preg_replace('/([[:space:]]+)?-([[:space:]]+)?/', '-',
$string);
--
Richard Heyes
http://www.heyes-computing.net/scripts/
Plain text email only please!