Skip to main content

replace function js to php



I am trying to change a js program to php. there is a replace function like $t = t.replace(/B/g, "b");





if i change this to php as $t = str_ireplace(/B/g, "b",$t);





it shows error about "unexpecting '/' ". how to solve this.


Comments

  1. First, JavaScript's replace function is closest to preg_replace.

    Second, if you read the docs for str_ireplace, it's case-insensitive replace, which given that you're trying to turn B into b is precisely the wrong function to use.

    You can do $t = str_replace('B', 'b', $t); (simpler) or $t = preg_replace('/B/', 'b', $t); (can handle more complex situations than what you're doing).

    ReplyDelete
  2. That call to Javascript replace uses a regular expression as the search criterion.

    The PHP Equivalent is preg_replace

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?