Categories: PHP

strtr – PHP String Functions

String strtr ( string, from, to );

OR

String strtr ( string, replace_array );


Description :

It’s an inbuilt function of PHP. strtr() function translate characters or replace strings.

In case if array is passed, This function will be the most efficient when all the keys have the same size.

Note: If from and to parameters have different lengths, the extra characters in the longer of the two are ignored. The length of string will be the same as the return value’s.


Parameter :

[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description

string, Required, String, Main string to be translated.

from, Required(if array is not used)String, Characters which needs to be translated from.

to, Required(if array is not used)String,  Characters which needs to be changed to to.

replace_array, Required(if to and from is not used), Array, This parameter represents the array containing respective From-to pairs Example  array(‘from’ => ‘to’) n times.

[/table]


Output :

It returns a translated or replaced string. Or in other words string in which all the characters of from sub string are replaced by to sub string in the given string.

If the replace_array parameter contains a key which is an empty string (“”), it will return FALSE.


Related articles : str_replace() , substr_replace().


strtr() – PHP Functions Example 1 :
<?php
echo strtr("He froj Tutorhal.","ejh","imi");
?>

In above example, We have string “He froj Tutorhal.”. from string “ejh” is changed to “imi”. So, the output of above code in the browser is as below:

Hi from Tutorial

strtr() – PHP Functions Example 2 : Using array as input.
<?php
$trans = array("hi" => "all", "I said hello" => "hi from tutorialmines.in");
echo strtr("Hello hi, I said hello", $trans); 
?>

In above example, The two modes of behavior are substantially different. With three arguments, strtr() will replace bytes; with two, it may replace longer substrings. So, the output of above code in the browser is as below:

Hello all, hi from tutorialmines.in

strtr() – PHP Functions Example 3 :  behavior comparison.
<?php
echo strtr("baab", "ab", "01"),"\n";
$trans = array("ab" => "01");
echo strtr("baab", $trans);
?>

Output of above code in the browser is as below:

11001
ba01

jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

7 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

7 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

7 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

7 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

7 months ago