---
warning: 'This file is auto generated by `npm run web:inject`, do not edit by hand'
examples:
  - 'str_replace('' '', ''.'', ''Kevin van Zonneveld'')'
  - 'str_replace([''{name}'', ''l''], [''hello'', ''m''], ''{name}, lars'')'
  - 'str_replace(Array(''S'',''F''),''x'',''ASDFASDF'')'
  - |-
    var countObj = {}
    str_replace(['A','D'], ['x','y'] , 'ASDFASDF' , countObj)
    var $result = countObj.value
estarget: es5
returns:
  - '''Kevin.van.Zonneveld'''
  - '''hemmo, mars'''
  - '''AxDxAxDx'''
  - '4'
dependencies: []
authors:
  original by:
    - 'Kevin van Zonneveld (https://kvz.io)'
  improved by:
    - Gabriel Paderni
    - Philip Peterson
    - 'Simon Willison (https://simonwillison.net)'
    - 'Kevin van Zonneveld (https://kvz.io)'
    - 'Onno Marsman (https://twitter.com/onnomarsman)'
    - 'Brett Zamir (https://brett-zamir.me)'
  bugfixed by:
    - Anton Ongson
    - 'Kevin van Zonneveld (https://kvz.io)'
    - Oleg Eremeev
    - 'Glen Arason (https://CanadianDomainRegistry.ca)'
    - 'Glen Arason (https://CanadianDomainRegistry.ca)'
  revised by:
    - 'Jonas Raoni Soares Silva (https://www.jsfromhell.com)'
  input by:
    - 'Onno Marsman (https://twitter.com/onnomarsman)'
    - 'Brett Zamir (https://brett-zamir.me)'
    - Oleg Eremeev
notes:
  - >-
    The countObj parameter (optional) if used must be passed in as a

    object. The count will then be written by reference into it's `value`
    property
type: function
layout: function
title: PHP's str_replace in JavaScript
description: >-
  Here’s what our current JavaScript equivalent to <a
  href="https://php.net/manual/en/function.str-replace.php">PHP's str_replace</a>
  looks like.
function: str_replace
category: strings
language: php
permalink: php/strings/str_replace/
alias:
  - /functions/php/str_replace/
  - /functions/strings/str_replace/
  - /php/str_replace/
  - /functions/str_replace/
---
{% codeblock lang:javascript %}module.exports = function str_replace (search, replace, subject, countObj) { // eslint-disable-line camelcase
  //  discuss at: https://locutus.io/php/str_replace/
  // original by: Kevin van Zonneveld (https://kvz.io)
  // improved by: Gabriel Paderni
  // improved by: Philip Peterson
  // improved by: Simon Willison (https://simonwillison.net)
  // improved by: Kevin van Zonneveld (https://kvz.io)
  // improved by: Onno Marsman (https://twitter.com/onnomarsman)
  // improved by: Brett Zamir (https://brett-zamir.me)
  //  revised by: Jonas Raoni Soares Silva (https://www.jsfromhell.com)
  // bugfixed by: Anton Ongson
  // bugfixed by: Kevin van Zonneveld (https://kvz.io)
  // bugfixed by: Oleg Eremeev
  // bugfixed by: Glen Arason (https://CanadianDomainRegistry.ca)
  // bugfixed by: Glen Arason (https://CanadianDomainRegistry.ca)
  //    input by: Onno Marsman (https://twitter.com/onnomarsman)
  //    input by: Brett Zamir (https://brett-zamir.me)
  //    input by: Oleg Eremeev
  //      note 1: The countObj parameter (optional) if used must be passed in as a
  //      note 1: object. The count will then be written by reference into it's `value` property
  //   example 1: str_replace(' ', '.', 'Kevin van Zonneveld')
  //   returns 1: 'Kevin.van.Zonneveld'
  //   example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars')
  //   returns 2: 'hemmo, mars'
  //   example 3: str_replace(Array('S','F'),'x','ASDFASDF')
  //   returns 3: 'AxDxAxDx'
  //   example 4: var countObj = {}
  //   example 4: str_replace(['A','D'], ['x','y'] , 'ASDFASDF' , countObj)
  //   example 4: var $result = countObj.value
  //   returns 4: 4

  var i = 0
  var j = 0
  var temp = ''
  var repl = ''
  var sl = 0
  var fl = 0
  var f = [].concat(search)
  var r = [].concat(replace)
  var s = subject
  var ra = Object.prototype.toString.call(r) === '[object Array]'
  var sa = Object.prototype.toString.call(s) === '[object Array]'
  s = [].concat(s)

  var $global = (typeof window !== 'undefined' ? window : global)
  $global.$locutus = $global.$locutus || {}
  var $locutus = $global.$locutus
  $locutus.php = $locutus.php || {}

  if (typeof (search) === 'object' && typeof (replace) === 'string') {
    temp = replace
    replace = []
    for (i = 0; i < search.length; i += 1) {
      replace[i] = temp
    }
    temp = ''
    r = [].concat(replace)
    ra = Object.prototype.toString.call(r) === '[object Array]'
  }

  if (typeof countObj !== 'undefined') {
    countObj.value = 0
  }

  for (i = 0, sl = s.length; i < sl; i++) {
    if (s[i] === '') {
      continue
    }
    for (j = 0, fl = f.length; j < fl; j++) {
      temp = s[i] + ''
      repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0]
      s[i] = (temp).split(f[j]).join(repl)
      if (typeof countObj !== 'undefined') {
        countObj.value += ((temp.split(f[j])).length - 1)
      }
    }
  }
  return sa ? s : s[0]
}
{% endcodeblock %}