PHP strsrt() Function with Example

By IncludeHelp Last updated : December 27, 2023

PHP strsrt() Function

The strsrt() function is a string function in PHP, it is used to replaces characters in the string. In this function, we can provide multiple characters to be replaced with the set of new multiple characters to replace with. (Example: if we want to replace "a" with "x" and "b" with "y" – then we can pass "ab" to replace with "xy").


The syntax of the strsrt() function:

strstr(string, old, new);


The parameters of the strsrt() function:

  • string is the main string, in which we have to replace certain characters.
  • old is the set of characters to be replaced.
  • new is the set of characters to replace.

Return Value

The return value of this method is string, it returns part of haystack string starting from and including the first occurrence of needle to the end of haystack. [Source]

Sample Input/Output

str = "This is a Game.";
old = "ia"
new = "eo"
Thes es o Gome.

str = "This is a Game.";
old = " ."
new = "_#"

Example of PHP strsrt() Function

$str = "This is a Game.";
//replacing "i" with "e" and "a" with "0"
echo (strtr($str,"ia","eo") . "\n");

$str = "This is a Game.";
//replacing space with "_" and dot (.) with "#"
echo (strtr($str," .","_#") . "\n");


The output of the above example is:

Thes es o Gome.

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.