Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12844

Re: strpos() before str_replace()? Or, maybe strtr()?

Message-ID <2411212.LVumIkLPpi@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2014-02-16 17:23 +0100
Subject Re: strpos() before str_replace()? Or, maybe strtr()?
Newsgroups comp.lang.php
References <cd42c387-7d7c-43de-bb0f-e12134d6ece2@googlegroups.com> <5300cef8$0$6662$9b4e6d93@newsspool2.arcor-online.net>

Show all headers | View raw


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.


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@94.75.214.39>

Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

strpos() before str_replace()? Or, maybe strtr()? Jason C <jwcarlton@gmail.com> - 2014-02-16 01:49 -0800
  Re: strpos() before str_replace()? Or, maybe strtr()? bill <william@TechServSys.com> - 2014-02-16 07:10 -0500
  Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-16 14:55 +0100
  Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-16 15:10 +0100
    Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:24 +0000
      Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-16 21:41 +0100
        Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:51 +0000
          Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-16 22:16 +0100
            Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 23:13 +0000
              Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-18 01:33 +0100
          Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-17 06:35 +0100
            Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-17 14:14 +0000
              Re: strpos() before str_replace()? Or, maybe strtr()? "J.O. Aho" <user@example.net> - 2014-02-17 18:55 +0100
            Re: strpos() before str_replace()? Or, maybe strtr()? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-17 19:30 +0100
  Re: strpos() before str_replace()? Or, maybe strtr()? Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-16 09:15 -0500
  Re: strpos() before str_replace()? Or, maybe strtr()? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-16 15:45 +0100
    Re: strpos() before str_replace()? Or, maybe strtr()? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-02-16 17:23 +0100
      Re: strpos() before str_replace()? Or, maybe strtr()? Christoph Michael Becker <cmbecker69@arcor.de> - 2014-02-16 17:41 +0100
  Re: strpos() before str_replace()? Or, maybe strtr()? Norman Peelman <npeelman@cfl.rr.com> - 2014-02-16 10:08 -0500
    Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 19:21 +0000
      Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:53 +0000
        Re: strpos() before str_replace()? Or, maybe strtr()? Denis McMahon <denismfmcmahon@gmail.com> - 2014-02-17 01:44 +0000
          Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-17 02:19 +0000
      Re: strpos() before str_replace()? Or, maybe strtr()? Norman Peelman <npeelman@cfl.rr.com> - 2014-02-16 18:17 -0500
  Re: strpos() before str_replace()? Or, maybe strtr()? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-02-16 20:49 +0000

csiph-web