Replace only once
taken from http://www.php.net/manual/en/function.str-replace.php
10970
protected static function strReplaceOnce($search, $replace, $subject)
    {
        $firstChar = strpos($subject, $search);
        if (false !== $firstChar) {
            $beforeStr = substr($subject, 0, $firstChar);
            $afterStr = substr($subject, $firstChar + strlen($search));

            return $beforeStr.$replace.$afterStr;
        }

        return $subject;
    }
743345
static function ReplaceFirst($s,$r,$str)
	{
		$l = strlen($str);
		$a = strpos($str,$s);
		$b = $a + strlen($s);
		$temp = substr($str,0,$a) . $r . substr($str,$b,($l-$b));
		return $temp;
	}
1226753
static function noAccents($s)
	{
		$s = str_replace(array('É', 'È', 'Ë', 'Ê'), 'E', $s);
		$s = str_replace(array('À', 'Ä', 'Â'), 'A', $s);
		$s = str_replace(array('Ï', 'Î'), 'I', $s);
		$s = str_replace(array('Ö', 'Ô'), 'O', $s);
		$s = str_replace(array('Ù', 'Ü' ,'Û'), 'U', $s);
		$s = str_replace('Ç', 'C', $s);
	
		$s = str_replace(array('é', 'è', 'ë', 'ê'), 'e', $s);
		$s = str_replace(array('à', 'ä', 'â'), 'a', $s);
		$s = str_replace(array('ï', 'î'), 'i', $s);
		$s = str_replace(array('ö', 'ô'), 'o', $s);
		$s = str_replace(array('ù', 'ü', 'û'), 'u', $s);
		$s = str_replace('ç', 'c', $s);
	
		return $s;
	}
622411
public function replace($strTag, $blnCache, $strCache, $flags, $tags, $arrCache, $index, $count)
    {
        $elements = explode('::', $strTag);

        switch (strtolower($elements[0]))
        {
            case 'trimsplit':
                if (!$elements[1] || !$elements[2] || is_array($elements[2]) || is_object($elements[2]))
                {
                    return '';
                }

                return serialize(trimsplit($elements[1], $elements[2]));

                break;
            case 'encrypt':
                if (!$elements[1])
                {
                    return '';
                }

                return \Encryption::hash($elements[1]);
            break;
        }

        return false;
    }
1084946
function strReplace()
    {
        if (($this->getParameter('needle') == null) || ($this->getParameter('haystack') == null))
            return $this->getParameter('haystack');

        $needle = $this->getParameter('needle');
        if($needle == '\n')
            $needle = "\n";

        return str_replace($needle, $this->getParameter('replace'), $this->getParameter('haystack'));
    }
296646
function ireplace ($search, $replace)
	{
		ensure('Search argument',  $search,  'is_a_string', __CLASS__, __METHOD__);
		ensure('Replace argument', $replace, 'is_a_string', __CLASS__, __METHOD__);

		$result = str_ireplace($search, $replace, $this);

		return S($result);
	}
42690
def replace(pretty, old_str, new_str):
    
    out_str = ''
    line_number = 1
    changes = 0
    for line in pretty.splitlines(keepends=True):
        new_line = line.replace(old_str, new_str)
        if line.find(old_str) != -1:
            logging.debug('%s', line_number)
            logging.debug('< %s', line)
            logging.debug('> %s', new_line)
            changes += 1
        out_str += new_line
        line_number += 1

    logging.info('Total changes(%s): %s', old_str, changes)
    return out_str
492135
static function strfilter($str)
    {
        $str = str_replace(' ', '_', $str);

        $str = strip_tags($str);

        $str = htmlspecialchars($str);

        return $str;
    }
1222454
def replace_param_occurrences(string, params):
    """"""
    for k, v in params.items():
        string = string.replace(k, str(v))
    return string
1315407
public function searchAndReplace()
	{
		$this->search();

		$this->replacement = new Replacement($this->formatted, $this->str);

		$this->replacement->replace();
		
		return $this->replacement->getStr();
	}
804698
def replace_first(input, string, replacement = ''.freeze)
      input.to_s.sub(string.to_s, replacement.to_s)
    end
1283920
function replaceCharacter(search, replace, subject, count) {
  var i = 0, j = 0, temp = '', repl = '', sl = 0,
    fl = 0, f = [].concat(search), r = [].concat(replace),
    s = subject, ra = Object.prototype.toString.call(r) === '[object Array]',
    sa = Object.prototype.toString.call(s) === '[object Array]', s = [].concat(s);
  
  if(typeof(search) === 'object' && typeof(replace) === 'string' ) {
    temp = replace; 
    replace = new Array();
    for (i=0; i < search.length; i+=1) { 
      replace[i] = temp; 
    }
    temp = ''; 
    r = [].concat(replace); 
    ra = Object.prototype.toString.call(r) === '[object Array]';
  }
  
  if (count) {
    this.window[count] = 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 (count) {
        this.window[count] += ((temp.split(f[j])).length - 1);
      } 
    }
  }
  return sa ? s : s[0];
}
1348935
function replace($search,$replace){
		$this->_String = $this->toString();
		return parent::replace($search,$replace);
	}
77999
def replaceStrs(s, *args):
    

    """
    if args == (): return s
    mapping = dict((frm, to) for frm, to in args)
    return re.sub("|".join(map(re.escape, mapping.keys())),
                  lambda match:mapping[match.group(0)], s)
1370732
public static function strIreplace($search, $replace, $str, $count = null, $encoding = null)
    {
        $encoding = $encoding === null ? mb_internal_encoding() : $encoding;

        if (!is_array($search)) {
            $slen = strlen($search);

            if ($slen == 0) {
                return $str;
            }

            $lendif = strlen($replace) - strlen($search);
            $search = static::strtolower($search, $encoding);
            $search = preg_quote($search, '/');
            $lstr = static::strtolower($str, $encoding);
            $i = 0;
            $matched = 0;

            while (preg_match('/(.*)' . $search . '/Us', $lstr, $matches)) {
                if ($count !== null && $i === $count) {
                    break;
                }

                $mlen = strlen($matches[0]);
                $lstr = substr($lstr, $mlen);

                $str = substr_replace($str, $replace, $matched + strlen($matches[1]), $slen);
                $matched += $mlen + $lendif;
                $i++;
            }

            return $str;
        } else {
            $keys = array_keys($search);

            foreach ($keys as $k) {
                if (is_array($replace)) {
                    if (array_key_exists($k, $replace)) {
                        $str = static::strIreplace($search[$k], $replace[$k], $str, $count, $encoding);
                    } else {
                        $str = static::strIreplace($search[$k], '', $str, $count, $encoding);
                    }
                } else {
                    $str = static::strIreplace($search[$k], $replace, $str, $count, $encoding);
                }
            }

            return $str;
        }
    }
1168561
public static function substr_replace($str, $repl, $start, $length = null)
    {
        preg_match_all('/./us', $str, $ar);
        preg_match_all('/./us', $repl, $rar);
        if ($length === null) {
            $length = AnHelperString::strlen($str);
        }
        array_splice($ar[0], $start, $length, $rar[0]);
        return join('', $ar[0]);
    }
653168
static String fastReplace( String str, String target, String replacement ) {
        int targetLength = target.length();
        if( targetLength == 0 ) {
            return str;
        }
        int idx2 = str.indexOf( target );
        if( idx2 < 0 ) {
            return str;
        }
        StringBuilder buffer = new StringBuilder( targetLength > replacement.length() ? str.length() : str.length() * 2 );
        int idx1 = 0;
        do {
            buffer.append( str, idx1, idx2 );
            buffer.append( replacement );
            idx1 = idx2 + targetLength;
            idx2 = str.indexOf( target, idx1 );
        } while( idx2 > 0 );
        buffer.append( str, idx1, str.length() );
        return buffer.toString();
    }