Home » PHP

PHP strsrt() Function with Example

PHP strsrt() Function: Here, we are going to learn about the strsrt() function with example in PHP.
Submitted by IncludeHelp, on February 06, 2019

PHP strsrt() Function

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:

    strstr(string, old, new);

Here,

  • 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.

Examples:

    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#





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.