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").

Syntax

The syntax of the strsrt() function:

strstr(string, old, new);

Parameters

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

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

Input:
str = "This is a Game.";
old = " ."
new = "_#"
Output:
This_is_a_Game#

Example of PHP strsrt() Function

<?php
$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");
?>

Output

The output of the above example is:

Thes es o Gome.
This_is_a_Game#

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 www.includehelp.com. All rights reserved.