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 #184926 is a reply to message #184925] Sun, 16 February 2014 16:41 Go to previous messageGo to previous message
Christoph Michael Bec is currently offline  Christoph Michael Bec
Messages: 207
Registered: June 2013
Karma:
Senior Member
Thomas 'PointedEars' Lahn wrote:

> Christoph Michael Becker wrote:
>
>> Jason C wrote:
>>> 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);
>>> }
>>>
>>> […]
>>> Or, would strtr() be faster? I've never actually used this function in
>>> practice; I think it's a slow function, but have never tested it, and
>>> don't remember where I read that:
>>>
>>> $firstname = strtr($firstname, $userArr);
>>> $lastname = strtr($lastname, $userArr);
>>>
>>> Any other suggestions on what might be the fastest option?
>>
>> Having the fastest option is surely less important than having a correct
>> solution, and there is an important semantic difference between
>> str_replace() and strtr(). Consider the following snippet:
>>
>> $array = array('some' => 'any', 'anything' => 'nothing');
>> $name = 'something';
>>
>> $keys = array_keys($array);
>> $values = array_values($array);
>> var_dump(str_replace($keys, $values, $name));
>>
>> var_dump(strtr($name, $array));
>>
>> This will output:
>>
>> string(7) "nothing"
>> string(8) "anything"
>
> There *is* a semantic difference. He did not call str_replace() with array
> parameters, though, nor did he suggest so.

Well, calling str_replace() in a loop as done by the OP has the same
semantic as calling str_replace() once with array parameters:

$array = array('some' => 'any', 'anything' => 'nothing');
$name = 'something';
foreach ($array as $key => $value) {
$name = str_replace($key, $value, $name);
}
var_dump($name); // string(7) "nothing"

(Using '****' instead of $value in the str_replace() might have similar
issues.)

Anyway, it is not clear to me what the OP wants to accomplish exactly.

--
Christoph M. Becker
[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: Fri Nov 22 05:22:31 GMT 2024

Total time taken to generate the page: 0.04418 seconds