RE: [PHP-DEV] Proposed additional behaviour of str_replace()
| From: | Mårten Gustafsson | Date: | Wed, 17 Apr 2002 23:12:30 +0000 |
| Subject: | RE: [PHP-DEV] Proposed additional behaviour of str_replace() | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-82953@lists.php.net to get a copy of this message | ||
> On Wed, Apr 17, 2002 at 01:01:29AM +0200, Mårten Gustafsson wrote :
> > Is there anyone else than me that would find it useful to be able to do:
> >
> > <?php
> > $string = 'foo bar';
> > $replacements = array(
> > 'foo' => 'FOO',
> > 'bar' => 'BAR');
> > // Prints "FOO BAR"
> > echo str_replace($replacements, $string);
> > ?>
> >
> > With current behaviour one has to do:
> >
> > <?php
> > $string = 'foo bar';
> > $replacements = array(
> > 'foo' => 'FOO',
> > 'bar' => 'BAR');
> > // Prints "FOO BAR"
> > echo str_replace(array_keys($replacements), array_values($string));
> > ?>
>
> The syntax is str_replace(search, replace, subject), btw.
Of course :)
> Personally, I don't see a need for this. On the other side,
> if it gets done, think about BC.
My point, which was not clear, was the ablity to provide one array with all needles and
replacements. And yep, it would have to be a new function to preserve BC...but since I´m,
obviously, blind I havn´t noticed that strtr() does exactly what I asked for :)
Marten.