replace

Modified on Thu, 9 Jul at 10:49 AM

replace

TABLE OF CONTENTS

Replace parts of strings

Definition

string replace(string $input, string $old, string $new)

Replaces occurrences of $old within $input with $new and return the resulting string.

Parameters

string $input

String to replace substrings in.

string $old

Substring of $input to be replaced.

string $new

String to replace the occurrences of $old within $input with.

Returns

string

$input with all the occurrences of $old replaced with $new.

Examples

Transformation
{
  a: replace('This is wrong!', 'wrong', 'correct'),
  b: replace('Line1\nLine2', '\n', ''),
  c: replace('-Hyphenated-String-', '-',`null`)
}
Result
{
  "a": "This is correct!",
  "b": "Line1Line2",
  "c": "HyphenatedString"
}

? When replacing new line symbols from sample data, use "\n" to signify them instead of '\n'.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article