# New String Function: str_replace_limit

- Thread: https://externals.io/message/61253
- Original archive: https://news-web.php.net/php.internals/61253
- Started by Paul Dragoonis on 2012-07-15T19:45:16+00:00
- Messages: 8
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #61253 — Paul Dragoonis — 2012-07-15T19:45:16+00:00

URL: https://externals.io/message/61253#61253

Hey,

I'm proposing to add a new function str_replace_limit, this will be
identical to `str_replace()` with one key difference, you can specify
how many times you want the replace to occur.

Currently this isn't possible with any functions in the
/ext/standard/string.c stack, the only easy workaround is using
 `preg_replace()` which requires of course the pcre library and regex
patterns.

I would have added this as a 4th param to `str_replace()`, but it
already has a 4th by-ref param to tell you how many times it done the
replacement.

mixed str_replace_limit ( mixed $search , mixed $replace , mixed
$subject [, int $limit ] )

Thoughts?

Paul Dragoonis.

---

## #61254 — Felipe Pena — 2012-07-15T19:54:53+00:00

URL: https://externals.io/message/61253#61254 · In reply to: #61253

Hi,

2012/7/15 Paul Dragoonis <dragoonis@gmail.com>:
> Hey,
>
> I'm proposing to add a new function str_replace_limit, this will be
> identical to `str_replace()` with one key difference, you can specify
> how many times you want the replace to occur.
>
> Currently this isn't possible with any functions in the
> /ext/standard/string.c stack, the only easy workaround is using
> `preg_replace()` which requires of course the pcre library and regex
> patterns.
>
> I would have added this as a 4th param to `str_replace()`, but it
> already has a 4th by-ref param to tell you how many times it done the
> replacement.
>
> mixed str_replace_limit ( mixed $search , mixed $replace , mixed
> $subject [, int $limit ] )
>
> Thoughts?
>

Surely a 5th param is preferred.

-- 
Regards,
Felipe Pena

---

## #61255 — Paul Dragoonis — 2012-07-15T19:59:10+00:00

URL: https://externals.io/message/61253#61255 · In reply to: #61254

The 4th param to str_replace is a by-ref param, so you can't just skip
over it, can you ?


> Hi,
>
> 2012/7/15 Paul Dragoonis <dragoonis@gmail.com>:
> > […]
>
> Surely a 5th param is preferred.
>
> --
> Regards,
> Felipe Pena

---

## #61275 — Gustavo Lopes — 2012-07-16T08:05:02+00:00

URL: https://externals.io/message/61253#61275 · In reply to: #61255

> The 4th param to str_replace is a by-ref param, so you can't just 
> skip
> over it, can you ?
>

I don't think so, but we could make it so that you could by using 
optional passing by reference.

-- 
Gustavo Lopes

---

## #61276 — Kingsquare.nl - Robin Speekenbrink — 2012-07-16T08:08:34+00:00

URL: https://externals.io/message/61253#61276 · In reply to: #61275

Hi all,

I'm a strict follower of this list and havent posted (as of yet) due to 
the fact that i'd like to be in the loop, but am not a C developer... :)

But on this discussion i'd like to give my 2c: As a PHP developer i'd 
regret to see yet another function added to the language instead of 
adding a new parameter. (this it does the same thing just limited) Also 
with the other discussion on being able to skip default parameters 
(using the keyword 'default') this shouldnt be a problem should it?

Thanks, i'll butt out now :D

Op maandag 16 juli 2012 10:05:02, Gustavo Lopes schreef:
> 
> > […]
>
> I don't think so, but we could make it so that you could by using
> optional passing by reference.

---

## #61277 — Paul Dragoonis — 2012-07-16T08:29:17+00:00

URL: https://externals.io/message/61253#61277 · In reply to: #61276

Thanks for the comments guys, I like your idea about skipping the
by-ref count parameter.

If the 'default' keyword were to be added in, then you could skip the
4th parameter and add your 5th 'limit' option.

$str = str_replace($search, $replace, $subject, default, 5);

Does anyone have any objections on this implementation approach?
(obviously we'd have to wait for the default param skipping to be
implemented, I'm not sure on the implementation status of that
feature)

Thanks.
Paul Dragoonis.

On Mon, Jul 16, 2012 at 9:08 AM, Kingsquare.nl - Robin Speekenbrink
<robin@kingsquare.nl> wrote:
> Hi all,
>
> I'm a strict follower of this list and havent posted (as of yet) due to the
> fact that i'd like to be in the loop, but am not a C developer... :)
>
> But on this discussion i'd like to give my 2c: As a PHP developer i'd regret
> to see yet another function added to the language instead of adding a new
> parameter. (this it does the same thing just limited) Also with the other
> discussion on being able to skip default parameters (using the keyword
> 'default') this shouldnt be a problem should it?
>
> Thanks, i'll butt out now :D
>
> Op maandag 16 juli 2012 10:05:02, Gustavo Lopes schreef:
>
> > […]

---

## #61286 — Florian Anderiasch — 2012-07-16T11:23:43+00:00

URL: https://externals.io/message/61253#61286 · In reply to: #61277

> Thanks for the comments guys, I like your idea about skipping the
> by-ref count parameter.
> 
> If the 'default' keyword were to be added in, then you could skip the
> 4th parameter and add your 5th 'limit' option.
> 
> $str = str_replace($search, $replace, $subject, default, 5);
> 
> Does anyone have any objections on this implementation approach?
> (obviously we'd have to wait for the default param skipping to be
> implemented, I'm not sure on the implementation status of that
> feature)

I'm not seeing what's the problem with it being a by-ref variable, as
you've mentioned it twice now. Are you bothered by an additional
variable being set in memory when skipping (i.e. not using it)? Or am I
reading more into it than you tried to say? :)

Greetings,
Florian

---

## #61287 — Paul Dragoonis — 2012-07-16T11:30:39+00:00

URL: https://externals.io/message/61253#61287 · In reply to: #61286

On Mon, Jul 16, 2012 at 12:23 PM, Florian Anderiasch
<florian@anderiasch.de> wrote:
> 
> > […]
>
> I'm not seeing what's the problem with it being a by-ref variable, as
> you've mentioned it twice now. Are you bothered by an additional
> variable being set in memory when skipping (i.e. not using it)? Or am I
> reading more into it than you tried to say? :)

I believe you were reading more into it.

I mention by-ref so it's easy to know if i'm talking about the
existing or new proposed param.

I'm not bothered by the by-ref param being skipped over to get to the
5th one, it's a hit I'm willing to take to achieve necessary
functionality on `str_replace()`.

If nobody has any further concerns I'll go ahead and create this new
parameter and provide a PR on github accordingly.

Paul Dragoonis.

>
> Greetings,
> Florian
