-1

I have a string, lets assume the string looks like "000110010111000011".

I would want to replace any occurrence of the number 1 in the string without limiting it to the first or last occurrence. I also want the string to return back the way it was, if needing to split it or something.

Joe
  • 957
  • 1
  • 12
  • 25
  • Have you looked into php's [`str_replace()`](http://php.net/manual/en/function.str-replace.php)?.. – Aiias Jun 09 '13 at 04:12

1 Answers1

1

You can do that using the str_replace() PHP function. Here's how you can change all occurences of '1' in the string with '2':

$newString = str_replace('1', '2', '000110010111000011');

The $newString variable will contain the modified string

anubhava
  • 713,503
  • 59
  • 514
  • 593
Filippos Karapetis
  • 4,317
  • 20
  • 38
  • I don't think you understand. I want to replace only one occurrence not limiting it to the first or last. It can be first or last, I just don't want to limit it to either or. – Joe Jun 09 '13 at 04:31
  • Check this question: http://stackoverflow.com/questions/9598665/php-replace-first-occurence-of-string-from-0th-position - in particular, the str_replace_once() function defined there – Filippos Karapetis Jun 09 '13 at 04:32
  • I don't want to limit it to the first occurrence. – Joe Jun 09 '13 at 21:22