Re: strpos() before str_replace()? Or, maybe strtr()? [message #184932 is a reply to message #184927] |
Sun, 16 February 2014 20:53 |
Ben Bacarisse
Messages: 82 Registered: November 2013
Karma:
|
Member |
|
|
Ben Bacarisse <ben(dot)usenet(at)bsb(dot)me(dot)uk> writes:
> Norman Peelman <npeelman(at)cfl(dot)rr(dot)com> writes:
>
>> On 02/16/2014 04:49 AM, Jason C wrote:
> <snip>
>>> foreach ($userArr as $key => $val) {
>>> if (strpos($firstname, $key) !== false)
>>> $firstname = str_replace($key, "****", $firstname);
>>>
>>> if (strpos($lastname, $key) !== false)
>>> $lastname = str_replace($key, "****", $lastname);
>>> }
>>
>>
>> It's slower because you are actually performing the same function
>> twice on each iteration.
>
> Did you try it? In my tests it's faster (37ms vs 66ms). This may be
> because of the assignment always being done.
I'm pretty sure that it is simply the overhead of str_replace compared
to that of strpos, and not to do with the assignment.
<snip>
--
Ben.
|
|
|