JString::str_ireplace() - usage of $count differs from PHP str_ireplace() function

26 views
Skip to the first unread message

webamoeba

unread,
10 Apr 2010, 05:48:0910/04/2010
to Joomla! Framework Development
I was working with JString::str_ireplace() in the trunk (1.6), and
noticed that the $count parameter does not work as expected.

The $count parameter in the PHP function str_ireplace() is passed by
reference and is updated with the number of replacements made by the
function. The $count parameter in JString::str_ireplace() is used to
limit the number of replacements that can be made. This seems like a
major divergance in functionality...

This is the test I ran:

$subject = "ABCDEFABCDEF";
$search = "D";
$replace = ".";
$count = 1;

var_dump(JString::str_ireplace($search, $replace, $subject, $count));
var_dump($count);

var_dump(str_ireplace($search, $replace, $subject, $count));
var_dump($count);

And this is the result:

string 'ABC.EFABCDEF' (length=12)

int 1

string 'ABC.EFABC.EF' (length=12)

int 2

Notice that the result is different and so is the ultimate value of
$count.

There is a problem in that we cannot pass $count (an integer) by
reference (as in the PHP function) - I would have thought the best
solution would be to remove the $count parameter?

Mark Dexter

unread,
10 Apr 2010, 12:40:5910/04/2010
to joomla-dev...@googlegroups.com
Hi. I noticed this when I was recently writing the unit tests for JString. I wasn't sure the best way to handle it. I think the count parameter was added in PHP 5.0, so that is probably why it wasn't in there before. I think if possible we should implement this, but I agree that we should either make it work or remove it. Do you want to try to make a patch to fix this? Thanks. Mark


--
You received this message because you are subscribed to the Google Groups "Joomla! Framework Development" group.
To post to this group, send an email to joomla-dev...@googlegroups.com.
To unsubscribe from this group, send email to joomla-dev-frame...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/joomla-dev-framework?hl=en-GB.


webamoeba

unread,
11 Apr 2010, 09:34:0011/04/2010
to Joomla! Framework Development
I did think of an alternative after I posted. We could remove the
$count parameter, but add a static class variable in which we store
the number of replacements made in the last call to the
JString::str_ireplace() method. Thereby allowing for the same
functionality, but using a alightly different approach.

> > joomla-dev-frame...@googlegroups.com<joomla-dev-framework%2Bunsu...@googlegroups.com>

Mark Dexter

unread,
11 Apr 2010, 17:02:5111/04/2010
to joomla-dev...@googlegroups.com
That sounds like a workable approach to me, but I'm not that experienced in framework stuff, so it would be great if someone with more experience could chime in here with an opinion. Thanks. Mark

To unsubscribe from this group, send email to joomla-dev-frame...@googlegroups.com.

Ian MacLennan

unread,
11 Apr 2010, 17:51:2411/04/2010
to joomla-dev...@googlegroups.com
Might be an off day for me and so this might be a dumb question, but why can't we have $count passed in by reference?

Ian

Mark Dexter

unread,
11 Apr 2010, 19:29:0111/04/2010
to joomla-dev...@googlegroups.com
I *think* the problem is that the $count variable doesn't do what it says in the doc block. Looking at the code, it appears that the count variable limits the number of replaces instead of counting them. So I think we can either change the documentation and the unit tests or try to fix the function to actually return the number of replacements. Mark

Mark Dexter

unread,
11 Apr 2010, 19:34:5811/04/2010
to joomla-dev...@googlegroups.com
Hi. I did some more testing and confirmed that this is indeed the case. I propose that we change the documentation as follows:

@param int optional count value to limit the number of replacements

and change the unit tests to test this parameter. I've got a local change to this effect that I can commit if you agree. Thanks. Mark

webamoeba

unread,
12 Apr 2010, 05:47:5312/04/2010
to Joomla! Framework Development
@Mark:
I dislike the idea of changing the documentation. This method is
intended to be an alternative to the PHP function str_ireplace() and
as such should have the same functionality, or possibly reduced
functionality. Changing the purpose of a parameter is likely to cause
confusion.

@Ian:
Perhaps I too am having an off day... but I didn't think that you
could pass primitives by reference?

On Apr 12, 12:34 am, Mark Dexter <dextercow...@gmail.com> wrote:
> Hi. I did some more testing and confirmed that this is indeed the case. I
> propose that we change the documentation as follows:
>
> @param int optional count value to limit the number of replacements
>
> and change the unit tests to test this parameter. I've got a local change to
> this effect that I can commit if you agree. Thanks. Mark
>

> On Sun, Apr 11, 2010 at 4:29 PM, Mark Dexter <dextercow...@gmail.com> wrote:
> > I *think* the problem is that the $count variable doesn't do what it says
> > in the doc block. Looking at the code, it appears that the count variable
> > limits the number of replaces instead of counting them. So I think we can
> > either change the documentation and the unit tests or try to fix the
> > function to actually return the number of replacements. Mark
>

> >>> joomla-dev-frame...@googlegroups.com<joomla-dev-framework%2Bunsu...@googlegroups.com>


> >>> .
>
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/joomla-dev-framework?hl=en-GB.
>
> >>  --
> >> You received this message because you are subscribed to the Google Groups
> >> "Joomla! Framework Development" group.
> >> To post to this group, send an email to
> >> joomla-dev...@googlegroups.com.
> >> To unsubscribe from this group, send email to

> >> joomla-dev-frame...@googlegroups.com<joomla-dev-framework%2Bunsu...@googlegroups.com>

Sam Moffatt

unread,
12 Apr 2010, 06:13:0012/04/2010
to joomla-dev...@googlegroups.com
You pass variables by reference, what you put in that variable is up
to you. By default PHP5 passes objects around by reference instead of
making copies like it did in PHP4.

I would be in favour of aligning it, all it takes is the addition of a
& in the params and the relevant code to update it. It is a
functionality change however but it is bringing it in line with PHP's
function call and I feel that would be acceptable.

Sam Moffatt
http://pasamio.id.au

> To unsubscribe from this group, send email to joomla-dev-frame...@googlegroups.com.

Mark Dexter

unread,
12 Apr 2010, 11:23:0212/04/2010
to joomla-dev...@googlegroups.com
Well, if we want to change how the method works, that's fine. As I said, right now that parameter can be used to limit the number of substitutions. If someone wants to make a patch to change the way the method works to return a count of substitutions instead, that's fine with me. Mark

Ian MacLennan

unread,
12 Apr 2010, 11:35:1912/04/2010
to joomla-dev...@googlegroups.com
Do we need this method anymore?  How does the core str_ireplace work?  Does it handle UTF8 okay?  If it does, I would simply deprecate this and leave it.

Ian

webamoeba

unread,
12 Apr 2010, 15:13:2112/04/2010
to Joomla! Framework Development
Looks like I was having a off day ;) you can indeed pass primitives by
reference (I knew that... honest).

Many of JString methods look to depend on the 3rd party library
http://phputf8.sourceforge.net. I had a peek at the latest release and
the issue still exists. It also looks as though the project has lost
momentum there hasn't been any activity in an age.

However I did come across an alternative library http://docs.kohanaphp.com/core/utf8.
This includes some rather nice looking UTF-8 aware string functions
and it also looks to be written by the same person Harry Fuecks (it's
amazing how often his name crops up!). Perhaps we should be
considering a change to this (or a similar, more up-to-date) library
for UTF-8 string functions?

just a thought...

Note I am assuming that this method is still required since Kohana
includes it and Kohana is a PHP5 library.

On 12 Apr, 16:35, Ian MacLennan <ian.maclen...@joomla.org> wrote:
> Do we need this method anymore?  How does the core str_ireplace work?  Does
> it handle UTF8 okay?  If it does, I would simply deprecate this and leave
> it.
>
> Ian
>

> On Mon, Apr 12, 2010 at 11:23 AM, Mark Dexter <dextercow...@gmail.com>wrote:
>
> > Well, if we want to change how the method works, that's fine. As I said,
> > right now that parameter can be used to limit the number of substitutions.
> > If someone wants to make a patch to change the way the method works to
> > return a count of substitutions instead, that's fine with me. Mark
>

> >> <joomla-dev-framework%2Bunsu...@googlegroups.com<joomla-dev-framework%252Buns...@googlegroups.com>


>
> >> >> >>> .
>
> >> >> >>> For more options, visit this group at
> >> >> >>>http://groups.google.com/group/joomla-dev-framework?hl=en-GB.
>
> >> >> >>  --
> >> >> >> You received this message because you are subscribed to the Google
> >> Groups
> >> >> >> "Joomla! Framework Development" group.
> >> >> >> To post to this group, send an email to
> >> >> >> joomla-dev...@googlegroups.com.
> >> >> >> To unsubscribe from this group, send email to
> >> >> >> joomla-dev-frame...@googlegroups.com<joomla-dev-framework%2Bunsu...@googlegroups.com>

> >> <joomla-dev-framework%2Bunsu...@googlegroups.com<joomla-dev-framework%252Buns...@googlegroups.com>

Reply all
Reply to author
Forward
0 new messages