FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » strpos() before str_replace()? Or, maybe strtr()?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: strpos() before str_replace()? Or, maybe strtr()? [message #184933 is a reply to message #184931] Sun, 16 February 2014 21:16 Go to previous messageGo to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma:
Senior Member
Ben Bacarisse wrote:

> "J.O. Aho" <user(at)example(dot)net> writes:
>> On 16/02/14 21:24, Ben Bacarisse wrote:
>>> Thomas 'PointedEars' Lahn <PointedEars(at)web(dot)de> writes:
>>>> Jason C wrote:
>>> <snip>
>>>> > I'm looping through roughly 100,000 values:
>>>> >
>>>> > $userArr = array(
>>>> > "something" => "anything",
>>>> > ...
>>>> > );
>>>> >
>>>> > foreach ($userArr as $key => $val) {
>>>> > $firstname = str_replace($key, "****", $firstname);
>>>> > $lastname = str_replace($key, "****", $lastname);
>>>> > }
>>>> >
>>>> > Both $firstname AND $lastname could contain any of the keys, but could
>>>> > only potentially contain one key each. It's likely, though, that
>>>> > neither will contain any of the keys.
>>> <snip>
>>>> > Would it be faster to use strpos() before each str_replace(), like
>>>> > so?:
>>>> >
>>>> > 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);
>>>> > }
>>>>
>>>> Most certainly not.
>>>
>>> And yet, when I measured it, it was faster. How can that be?
>>>
>>> <snip>
>>
>> For strpos is faster than str_replace on finding the string.

How, and why? Both search for the occurrence of a substring in a string.

> Yes, though the question was rhetorical!

The question was stupid.


PointeDears
--
Sometimes, what you learn is wrong. If those wrong ideas are close to the
root of the knowledge tree you build on a particular subject, pruning the
bad branches can sometimes cause the whole tree to collapse.
-- Mike Duffy in cljs, <news:Xns9FB6521286DB8invalidcom(at)94(dot)75(dot)214(dot)39>
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: getting php mail error info
Next Topic: Declaring an array necessary?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 23:22:29 GMT 2024

Total time taken to generate the page: 0.04050 seconds