PHP strsrt() Function with Example

PHP strsrt() Function with Example

PHP strsrt() Function: Here, we will find out about the strsrt() work with the example in PHP.

PHP strsrt() Function

strsrt() work is a string capacity in PHP, it is utilized to replaces characters in the string. In this capacity, we can furnish various characters to be supplanted with the arrangement of new numerous characters to supplant with. (Example: in the event that we need to supplant “a” with “x” and “b” with “y” – at that point, we can pass “abdominal muscle” to supplant with “XY”).

Syntax:

strstr(string, old, new); 

Here,

  • the string is the primary string, where we need to supplant certain characters.
  • old is the arrangement of characters to be supplanted.
  • new is the arrangement of characters to supplant.

Example:

    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#

PHP Code:

<?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:

Thes es o Gome.
This_is_a_Game#

Recent Posts

error: Alert: Content is protected!!